Skip to content

Commit bd90747

Browse files
add 1080
1 parent e0f70f2 commit bd90747

File tree

3 files changed

+75
-0
lines changed
  • paginated_contents/algorithms/2nd_thousand
  • src

3 files changed

+75
-0
lines changed

paginated_contents/algorithms/2nd_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@
414414
| 1087 | [Brace Expansion](https://leetcode.com/problems/brace-expansion/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1087.java) | | Medium | Backtracking |
415415
| 1086 | [High Five](https://leetcode.com/problems/high-five/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1086.java) | [:tv:](https://www.youtube.com/watch?v=3iqC5J4l0Cc) | Easy ||
416416
| 1085 | [Sum of Digits in the Minimum Number](https://leetcode.com/problems/sum-of-digits-in-the-minimum-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1085.java) | [:tv:](https://www.youtube.com/watch?v=GKYmPuHZpQg) | Easy ||
417+
| 1080 | [Insufficient Nodes in Root to Leaf Paths](https://leetcode.com/problems/insufficient-nodes-in-root-to-leaf-paths/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1080.java) | | Medium |Tree, DFS
417418
| 1079 | [Letter Tile Possibilities](https://leetcode.com/problems/letter-tile-possibilities/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1079.java) | | Medium ||
418419
| 1078 | [Occurrences After Bigram](https://leetcode.com/problems/occurrences-after-bigram/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1078.java) | | Easy ||
419420
| 1071 | [Greatest Common Divisor of Strings](https://leetcode.com/problems/greatest-common-divisor-of-strings/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1071.java) | | Easy ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder.solutions.secondthousand;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
public class _1080 {
6+
public static class Solution1 {
7+
/**
8+
* Credit: https://leetcode.com/problems/insufficient-nodes-in-root-to-leaf-paths/solutions/1340243/concise-dfs-solution-cpp-and-java-0ms/
9+
* DFS does this very cleanly.
10+
*/
11+
public TreeNode sufficientSubset(TreeNode root, int limit) {
12+
return dfs(root, limit, 0);
13+
}
14+
15+
private TreeNode dfs(TreeNode root, int limit, int sumThusFar) {
16+
if (root == null) {
17+
return null;
18+
}
19+
if (root.left == null && root.right == null) {
20+
return root.val + sumThusFar < limit ? null : root;
21+
}
22+
root.left = dfs(root.left, limit, sumThusFar + root.val);
23+
root.right = dfs(root.right, limit, sumThusFar + root.val);
24+
return root.left == null && root.right == null ? null : root;
25+
}
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.fishercoder.secondthousand;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions.secondthousand._1080;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.util.Arrays;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
13+
public class _1080Test {
14+
private static _1080.Solution1 solution1;
15+
16+
@BeforeEach
17+
public void setup() {
18+
solution1 = new _1080.Solution1();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, -3, -5, null, 4, null));
24+
TreeUtils.printBinaryTree(root);
25+
TreeNode expected = TreeUtils.constructBinaryTree(Arrays.asList(1, null, -3, 4));
26+
TreeUtils.printBinaryTree(expected);
27+
assertEquals(expected, solution1.sufficientSubset(root, -1));
28+
}
29+
30+
@Test
31+
public void test2() {
32+
TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, -3, -5, 3, null, 4, null));
33+
TreeUtils.printBinaryTree(root);
34+
TreeNode expected = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, -3, null, 3, null, 4));
35+
TreeUtils.printBinaryTree(expected);
36+
assertEquals(expected, solution1.sufficientSubset(root, -1));
37+
}
38+
39+
@Test
40+
public void test3() {
41+
TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(5, 4, 8, 11, null, 17, 4, 7, 1, null, null, 5, 3));
42+
TreeUtils.printBinaryTree(root);
43+
TreeNode expected = TreeUtils.constructBinaryTree(Arrays.asList(5, 4, 8, 11, null, 17, 4, 7, null, null, null, 5));
44+
TreeUtils.printBinaryTree(expected);
45+
assertEquals(expected, solution1.sufficientSubset(root, 22));
46+
}
47+
}

0 commit comments

Comments
 (0)