Submission Detail

108 / 108 test cases passed.
Status:

Accepted

Runtime: 1 ms
Memory Usage: 40.2 MB
Submitted: 0 minutes ago
loading
Runtime Error Message:
Last executed input:
Input:
Output:
Expected:

Accepted Solutions Runtime Distribution

0
2
4
6
8
10
12
14
16
10
20
30
40
50
60
70
80
java
You are here!
Your runtime beats 31.20 % of java submissions.
Runtime (ms)
Distribution (%)

0
2
4
6
8
10
12
14
16
25
50
75
Zoom area by dragging across this chart

Accepted Solutions Memory Distribution

39400
39600
39800
40000
40200
40400
40600
40800
41000
2.5
5.0
7.5
10.0
12.5
15.0
java
You are here!
Your memory usage beats 61.69 % of java submissions.
Memory (KB)
Distribution (%)

39400
39600
39800
40000
40200
40400
40600
40800
41000
5
10
15
Zoom area by dragging across this chart

Invite friends to challenge Sum Root to Leaf Numbers


Submitted Code: 0 minutes ago

Language: 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
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int sumNumbers(TreeNode root) {
ArrayList<Integer> number = new ArrayList<>();
int sum = preOrder(root,number,0);
return sum;
}
public int preOrder(TreeNode root,ArrayList<Integer> number, int sum) {
if (root == null)
return sum;
number.add(root.val);
// end of branch detected
if (root.left == null && root.right == null) {
int pow = number.size()-1;
int num = 0;
for (int i = 0; i < number.size(); i++) {
num += number.get(i)*(Math.pow(10,pow));
pow--;
}
sum += num;
return sum;
}
// proceed with traversal
if (root.left != null) {
sum = preOrder(root.left,number,sum);
number.remove(number.size()-1);
}
if (root.right != null) {
sum = preOrder(root.right,number,sum);
number.remove(number.size()-1);
}
return sum;
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX