-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay86.java
More file actions
39 lines (32 loc) · 883 Bytes
/
Day86.java
File metadata and controls
39 lines (32 loc) · 883 Bytes
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
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public class Day86 {
public boolean checkSumProperty(TreeNode root) {
if (root == null || (root.left == null && root.right == null)) {
return true;
}
return isSumPropertyValid(root);
}
private boolean isSumPropertyValid(TreeNode node) {
if (node == null) {
return true;
}
if (node.left == null && node.right == null) {
return true;
}
int sum = 0;
if (node.left != null) {
sum += node.left.val;
}
if (node.right != null) {
sum += node.right.val;
}
return (node.val == sum) && isSumPropertyValid(node.left) && isSumPropertyValid(node.right);
}
}