Skip to content

Commit 7d8be5c

Browse files
add 666
1 parent 2ff0c9a commit 7d8be5c

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
25+
|666|[Path Sum IV](https://leetcode.com/problems/path-sum-iv/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_666.java) | O(1) | O(1) | Medium | Tree, DFS
2526
|665|[Non-decreasing Array](https://leetcode.com/problems/non-decreasing-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_665.java) | O(n) | O(n) | Easy |
2627
|664|[Strange Printer](https://leetcode.com/problems/strange-printer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_664.java) | O(n^3) | O(n^2) | Hard | DP
2728
|663|[Equal Tree Partition](https://leetcode.com/problems/equal-tree-partition/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_663.java) | O(n) | O(n) | Medium | Tree
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
/**
6+
* 666. Path Sum IV
7+
* If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digits integers.
8+
9+
For each integer in this list:
10+
11+
The hundreds digit represents the depth D of this node, 1 <= D <= 4.
12+
13+
The tens digit represents the position P of this node in the level it belongs to, 1 <= P <= 8.
14+
The position is the same as that in a full binary tree.
15+
16+
The units digit represents the value V of this node, 0 <= V <= 9.
17+
18+
Given a list of ascending three-digits integers representing a binary with the depth smaller than 5.
19+
You need to return the sum of all paths from the root towards the leaves.
20+
21+
Example 1:
22+
23+
Input: [113, 215, 221]
24+
Output: 12
25+
Explanation:
26+
The tree that the list represents is:
27+
3
28+
/ \
29+
5 1
30+
31+
The path sum is (3 + 5) + (3 + 1) = 12.
32+
33+
Example 2:
34+
35+
Input: [113, 221]
36+
Output: 4
37+
Explanation:
38+
The tree that the list represents is:
39+
3
40+
\
41+
1
42+
43+
The path sum is (3 + 1) = 4.
44+
45+
*/
46+
public class _666 {
47+
public static class Solution1 {
48+
/**OMG, since it's no larger than depth 5, I've got a hardcoded solution here....
49+
* By "harcoded", I mean the constructTree() method.*/
50+
public int totalSum = 0;
51+
52+
public int pathSum(int[] nums) {
53+
TreeNode root = constructTree(nums);
54+
if (root == null) {
55+
return 0;
56+
}
57+
computePathSum(root, 0);
58+
return totalSum;
59+
}
60+
61+
private void computePathSum(TreeNode root, int pathSum) {
62+
pathSum += root.val;
63+
if (root.left != null) {
64+
computePathSum(root.left, pathSum);
65+
}
66+
if (root.right != null) {
67+
computePathSum(root.right, pathSum);
68+
}
69+
if (root.left == null && root.right == null) {
70+
totalSum += pathSum;
71+
}
72+
// pathSum -= root.val;
73+
/**this line is not necessary as I'm passing pathSum as a local variable around, so it's always updated
74+
it's AC'ed with or without this line*/
75+
}
76+
77+
private TreeNode constructTree(int[] nums) {
78+
if (nums == null || nums.length == 0) {
79+
return null;
80+
}
81+
TreeNode root = new TreeNode(Integer.parseInt(Integer.toString(nums[0]).substring(2, 3)));
82+
//depth 2
83+
for (int i = 1; i < nums.length; i++) {
84+
if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 2 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 1) {
85+
root.left = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3)));
86+
} else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 2 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 2) {
87+
root.right = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3)));
88+
}
89+
}
90+
91+
//depth 3
92+
for (int i = 2; i < nums.length; i++) {
93+
if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 3 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 1) {
94+
root.left.left = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3)));
95+
} else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 3 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 2) {
96+
root.left.right = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3)));
97+
} else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 3 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 3) {
98+
root.right.left = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3)));
99+
} else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 3 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 4) {
100+
root.right.right = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3)));
101+
}
102+
}
103+
104+
//depth 4
105+
for (int i = 3; i < nums.length; i++) {
106+
if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 1) {
107+
root.left.left.left = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3)));
108+
} else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 2) {
109+
root.left.left.right = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3)));
110+
} else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 3) {
111+
root.left.right.left = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3)));
112+
} else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 4) {
113+
root.left.right.right = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3)));
114+
} else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 5) {
115+
root.right.left.left = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3)));
116+
} else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 6) {
117+
root.right.left.right = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3)));
118+
} else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 7) {
119+
root.right.right.left = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3)));
120+
} else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 8) {
121+
root.right.right.right = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3)));
122+
}
123+
}
124+
125+
return root;
126+
}
127+
}
128+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._666;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _666Test {
10+
private static _666.Solution1 solution1;
11+
private static int[] nums;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _666.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
nums = new int[]{113, 215, 221};
21+
solution1.totalSum = 0;
22+
assertEquals(12, solution1.pathSum(nums));
23+
}
24+
25+
@Test
26+
public void test2() {
27+
nums = new int[]{113, 221};
28+
solution1.totalSum = 0;
29+
assertEquals(4, solution1.pathSum(nums));
30+
}
31+
32+
@Test
33+
public void test3() {
34+
nums = new int[]{113, 214, 221, 348, 487};
35+
solution1.totalSum = 0;
36+
assertEquals(26, solution1.pathSum(nums));
37+
}
38+
39+
}

0 commit comments

Comments
 (0)