-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_965.java
31 lines (28 loc) · 997 Bytes
/
_965.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
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
public class _965 {
public static class Solution1 {
public boolean isUnivalTree(TreeNode root) {
if (root == null) {
return true;
}
return dfs(root, root.val);
}
private boolean dfs(TreeNode root, int value) {
if (root == null) {
return true;
}
if (root.val != value) {
return false;
}
return dfs(root.left, value) && dfs(root.right, value);
}
}
public static class Solution2 {
public boolean isUnivalTree(TreeNode root) {
boolean leftUnivaled = root.left == null || root.left.val == root.val && isUnivalTree(root.left);
boolean rightUnivaled = root.right == null || root.right.val == root.val && isUnivalTree(root.right);
return leftUnivaled && rightUnivaled;
}
}
}