-
Notifications
You must be signed in to change notification settings - Fork 22
/
IsBinaryTreeASumTree.java
45 lines (32 loc) · 1.18 KB
/
IsBinaryTreeASumTree.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
package com.company.amazon;
public class IsBinaryTreeASumTree {
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
BinaryTree.Node node = new BinaryTree.Node(26);
node.left = new BinaryTree.Node(11);
node.right = new BinaryTree.Node(3);
node.right.right = new BinaryTree.Node(3);
node.left.left = new BinaryTree.Node(4);
node.left.right = new BinaryTree.Node(6);
tree.root = node;
System.out.println("Is Sum Tree ?" + isSumTree(tree.root));
}
private static boolean isSumTree(BinaryTree.Node root) {
if (root == null || (root.left == null && root.right == null))
return true;
sum = 0;
int leftHeight = getHeight(root.left);
sum = 0;
int rightHeight = getHeight(root.right);
return (root.data == leftHeight + rightHeight) && isSumTree(root.left) && isSumTree(root.right);
}
static int sum = 0;
private static int getHeight(BinaryTree.Node root) {
if (root == null)
return sum;
getHeight(root.left);
sum += root.data;
getHeight(root.right);
return sum;
}
}