-
Notifications
You must be signed in to change notification settings - Fork 0
/
SumRootToLeaf.java
75 lines (57 loc) · 2.15 KB
/
SumRootToLeaf.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.mckinsey.engage.risk.services;
import java.util.LinkedList;
import java.util.List;
public class SumRootToLeaf {
public void main(String[] args) {
TreeNode tree = new TreeNode();
List<String> allNumbersInString = convertRootToLeadDigitsIntoNumber(tree);
int sum = 0;
for (String number : allNumbersInString) {
sum = sum + Integer.parseInt(number);
}
}
public int sumNumbers(TreeNode root) {
List<String> allNumbersInString = convertRootToLeadDigitsIntoNumber(root);
int sum = 0;
for (String number : allNumbersInString) {
sum = sum + Integer.parseInt(number);
}
return sum;
}
List<String> convertRootToLeadDigitsIntoNumber(TreeNode root) {
if (root == null)
return new LinkedList<>();
if (root.left == null && root.right == null) {
LinkedList<String> list = new LinkedList<>();
list.add(root.val + "");
return list;
}
List<String> rightSubtreeIntegers = convertRootToLeadDigitsIntoNumber(root.left);
List<String> leftSubtreeIntegers = convertRootToLeadDigitsIntoNumber(root.right);
List<String> rootList = new LinkedList<>();
for (String number : rightSubtreeIntegers) {
rootList.add(root.val + number);
}
for (String number : leftSubtreeIntegers) {
rootList.add(root.val + number);
}
return rootList;
}
Integer convertRootToLeadDigitsIntoNumber2nd(int sum, TreeNode root, int level) {
if (root == null)
return 0;
if (root.left == null && root.right == null) {
sum = root.val;
return sum;
}
int sumTillNow = (int) ((sum * Math.pow(10, level)) + root.val);
Integer rightSubtreeSum = convertRootToLeadDigitsIntoNumber2nd(sumTillNow, root.left, level + 1);
Integer leftSubtreeSum = convertRootToLeadDigitsIntoNumber2nd(sumTillNow, root.right, level + 1);
return rightSubtreeSum + leftSubtreeSum;
}
class TreeNode {
int val;
TreeNode left;
TreeNode right;
}
}