-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSumOfRootToLeafBinaryNumbers.java
More file actions
37 lines (26 loc) · 1.04 KB
/
Copy pathSumOfRootToLeafBinaryNumbers.java
File metadata and controls
37 lines (26 loc) · 1.04 KB
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
// Given a binary tree, each node has value 0 or 1.
// Each root-to-leaf path represents a binary number starting with the most significant bit.
// Return the sum of these numbers.
// See: https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/
package leetcode.tree;
import static leetcode.util.tree.BinTreeUtil.initTree;
import leetcode.util.tree.TreeNode;
public class SumOfRootToLeafBinaryNumbers {
public int sumRootToLeaf(TreeNode root) {
return helper(root, 0);
}
private int helper(TreeNode root, int sum) {
if (root == null) {
return 0;
}
if (root.left == null && root.right == null) {
return root.val + sum;
}
return helper(root.left, (root.val + sum) * 2) + helper(root.right, (root.val + sum) * 2);
}
public static void main(String[] args) {
SumOfRootToLeafBinaryNumbers sln = new SumOfRootToLeafBinaryNumbers();
TreeNode t1 = initTree(1, 0, 1, 0, 1, 0, 1);
System.out.println(sln.sumRootToLeaf((t1)));
}
}