-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSumRootToLeafNumbers.java
More file actions
61 lines (45 loc) · 1.59 KB
/
SumRootToLeafNumbers.java
File metadata and controls
61 lines (45 loc) · 1.59 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Given a binary tree containing digits from 0-9 only,
// each root-to-leaf path could represent a number.
// An example is the root-to-leaf path 1->2->3 which represents the number 123.
// Find the total sum of all root-to-leaf numbers.
// See: https://leetcode.com/problems/sum-root-to-leaf-numbers/
package leetcode.tree;
import static leetcode.util.tree.BinTreeUtil.*;
import leetcode.util.tree.TreeNode;
public class SumRootToLeafNumbers {
public int sumNumbers(TreeNode root) {
return helper(root, 0);
}
private int helper(TreeNode root, int curr) {
if (root == null) {
return 0;
}
if (root.left == null && root.right == null) {
return curr * 10 + root.val;
}
return helper(root.left, curr * 10 + root.val) +
helper(root.right, curr * 10 + root.val);
}
/**
* String concatenation solution
*/
public int sumNumbers1(TreeNode root) {
return dfs(root, "");
}
private int dfs(TreeNode root, String curr) {
if (root == null) {
return 0;
}
if (root.left == null && root.right == null) {
return Integer.parseInt(curr + root.val);
}
return dfs(root.left, curr + root.val) + dfs(root.right, curr + root.val);
}
public static void main(String[] args) {
SumRootToLeafNumbers sln = new SumRootToLeafNumbers();
TreeNode t1 = initTree(1, 2, 3);
// printInorder(t1);
System.out.println(sln.sumNumbers(t1));
System.out.println(sln.sumNumbers(null));
}
}