-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path129.java
More file actions
75 lines (72 loc) · 2.49 KB
/
129.java
File metadata and controls
75 lines (72 loc) · 2.49 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
__________________________________________________________________________________________________
sample 0 ms submission
class Solution {
public int sumNumbers(TreeNode root) {
if (root == null) return 0;
return sumNums(root, 0);
}
public int sumNums(TreeNode root, int path){
if (root == null) return 0;
path = (path*10) + root.val;
int left = sumNums(root.left, path);
int right = sumNums(root.right, path);
if (root.left == null && root.right == null){
return path;
}
return left + right;
}
}
__________________________________________________________________________________________________
sample 34128 kb submission
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int sumNumbers(TreeNode root) {
List<int[]> list = helper(root);
int sum = 0;
for(int[] i : list){
sum += i[0];
}
return sum;
}
public List<int[]> helper(TreeNode node) {
List<int[]> result = new ArrayList<int[]>();
if(node == null) return result;
if(node.left == null && node.right == null) {
result.add(new int[]{node.val, 1});
} else {
if(node.left != null) {
List<int[]> tempResult = helper(node.left);
for(int i=0; i<tempResult.size(); i++) {
int count = tempResult.get(i)[1] * 10;
int temp = tempResult.get(i)[0] + node.val * count;
result.add(new int[]{temp, count});
}
}
if(node.right != null) {
List<int[]> tempResult = helper(node.right);
for(int i=0; i<tempResult.size(); i++) {
int count = tempResult.get(i)[1] * 10;
int temp = tempResult.get(i)[0] + node.val * count;
result.add(new int[]{temp, count});
}
}
}
return result;
}
void merge(List<int[]> result, List<int[]> partial, int value) {
for(int i=0; i<partial.size(); i++) {
int count = partial.get(i)[1] * 10;
int temp = partial.get(i)[0] + value * count;
result.add(new int[]{temp, count});
}
}
}
__________________________________________________________________________________________________