-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCheckSumTree.java
36 lines (31 loc) · 916 Bytes
/
CheckSumTree.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
/*https://practice.geeksforgeeks.org/problems/sum-tree/1*/
class Solution
{
boolean result;
boolean isSumTree(Node root)
{
//assume the tree is a sum tree
result = true;
//check for any violation
postOrder(root);
//return the result
return result;
}
int postOrder(Node root)
{
//if the tree is still a sum tree and root is not null
if (result && root != null)
{
//find the left and right subtree sums
int leftSum = postOrder(root.left);
int rightSum = postOrder(root.right);
//if the root is not a leaf and also not equal to the sum of left and right subtrees
//store false in the result
if (root.left != null && root.right != null && root.data != leftSum+rightSum)
result = false;
//return the total sum
return leftSum+rightSum+root.data;
}
return 0;
}
}