-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
59 lines (55 loc) · 1.52 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package Tree.easy.No_965_Univalued_Binary_Tree;
import Tree.TreeNode;
import java.util.ArrayList;
import java.util.List;
/**
* FileName: Solution
* Author: mac
* Date: 2019-02-25 16:39
* Description: Univalued Binary Tree
* Recently Review: 2019-05-11 08:52
* <p>
* A binary tree is univalued if every node in the tree has the same value.
* <p>
* Return true if and only if the given tree is univalued.
* <p>
* <p>
* <p>
* Example 1:
* <p>
* <p>
* Input: [1,1,1,1,1,null,1]
* Output: true
* Example 2:
* <p>
* <p>
* Input: [2,2,2,5,2]
* Output: false
*/
public class Solution {
//法一:
public boolean isUnivalTree(TreeNode root) {
boolean isLeft = root.left == null || root.left.val == root.val && isUnivalTree(root.left);
boolean isRight = root.right == null || root.right.val == root.val && isUnivalTree(root.right);
return isLeft && isRight;
}
//法二:
private List<Integer> mContainer = new ArrayList<>();
public boolean isUnivalTree1(TreeNode root) {
handleNodeToArray(root);
for (int i = 0; i < mContainer.size(); i++) {
for (int j = 1;j < mContainer.size();j++){
if (!mContainer.get(i).equals(mContainer.get(j))){
return false;
}
}
}
return true;
}
private void handleNodeToArray(TreeNode root) {
if (root == null) return;
mContainer.add(root.val);
handleNodeToArray(root.left);
handleNodeToArray(root.right);
}
}