-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_666.java
168 lines (156 loc) · 7.99 KB
/
_666.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.fishercoder.solutions.firstthousand;
import com.fishercoder.common.classes.TreeNode;
import java.util.HashMap;
import java.util.Map;
public class _666 {
public static class Solution1 {
/*
* OMG, since it's no larger than depth 5, I've got a hardcoded solution here....
* By "harcoded", I mean the constructTree() method.
*/
public int totalSum = 0;
public int pathSum(int[] nums) {
TreeNode root = constructTree(nums);
if (root == null) {
return 0;
}
computePathSum(root, 0);
return totalSum;
}
private void computePathSum(TreeNode root, int pathSum) {
pathSum += root.val;
if (root.left != null) {
computePathSum(root.left, pathSum);
}
if (root.right != null) {
computePathSum(root.right, pathSum);
}
if (root.left == null && root.right == null) {
totalSum += pathSum;
}
// pathSum -= root.val;
/*this line is not necessary as I'm passing pathSum as a local variable around, so it's always updated
it's AC'ed with or without this line*/
}
private TreeNode constructTree(int[] nums) {
if (nums == null || nums.length == 0) {
return null;
}
TreeNode root =
new TreeNode(Integer.parseInt(Integer.toString(nums[0]).substring(2, 3)));
// depth 2
for (int i = 1; i < nums.length; i++) {
if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 2
&& Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 1) {
root.left =
new TreeNode(
Integer.parseInt(Integer.toString(nums[i]).substring(2, 3)));
} else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 2
&& Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 2) {
root.right =
new TreeNode(
Integer.parseInt(Integer.toString(nums[i]).substring(2, 3)));
}
}
// depth 3
for (int i = 2; i < nums.length; i++) {
if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 3
&& Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 1) {
root.left.left =
new TreeNode(
Integer.parseInt(Integer.toString(nums[i]).substring(2, 3)));
} else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 3
&& Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 2) {
root.left.right =
new TreeNode(
Integer.parseInt(Integer.toString(nums[i]).substring(2, 3)));
} else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 3
&& Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 3) {
root.right.left =
new TreeNode(
Integer.parseInt(Integer.toString(nums[i]).substring(2, 3)));
} else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 3
&& Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 4) {
root.right.right =
new TreeNode(
Integer.parseInt(Integer.toString(nums[i]).substring(2, 3)));
}
}
// depth 4
for (int i = 3; i < nums.length; i++) {
if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4
&& Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 1) {
root.left.left.left =
new TreeNode(
Integer.parseInt(Integer.toString(nums[i]).substring(2, 3)));
} else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4
&& Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 2) {
root.left.left.right =
new TreeNode(
Integer.parseInt(Integer.toString(nums[i]).substring(2, 3)));
} else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4
&& Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 3) {
root.left.right.left =
new TreeNode(
Integer.parseInt(Integer.toString(nums[i]).substring(2, 3)));
} else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4
&& Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 4) {
root.left.right.right =
new TreeNode(
Integer.parseInt(Integer.toString(nums[i]).substring(2, 3)));
} else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4
&& Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 5) {
root.right.left.left =
new TreeNode(
Integer.parseInt(Integer.toString(nums[i]).substring(2, 3)));
} else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4
&& Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 6) {
root.right.left.right =
new TreeNode(
Integer.parseInt(Integer.toString(nums[i]).substring(2, 3)));
} else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4
&& Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 7) {
root.right.right.left =
new TreeNode(
Integer.parseInt(Integer.toString(nums[i]).substring(2, 3)));
} else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4
&& Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 8) {
root.right.right.right =
new TreeNode(
Integer.parseInt(Integer.toString(nums[i]).substring(2, 3)));
}
}
return root;
}
}
public static class Solution2 {
public int totalSum = 0;
public int pathSum(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int key = nums[i] / 10;
int value = nums[i] % 10;
map.put(key, value);
}
dfs(nums[0] / 10, 0, map);
return totalSum;
}
private void dfs(int node, int preSum, Map<Integer, Integer> map) {
int level = node / 10;
int pos = node % 10;
int leftChild = (level + 1) * 10 + pos * 2 - 1;
int rightChild = (level + 1) * 10 + pos * 2;
int currSum = preSum + map.get(node);
if (!map.containsKey(leftChild) && !map.containsKey(rightChild)) {
totalSum += currSum;
return;
}
if (map.containsKey(leftChild)) {
dfs(leftChild, currSum, map);
}
if (map.containsKey(rightChild)) {
dfs(rightChild, currSum, map);
}
}
}
}