Skip to content

Commit 17a7ebb

Browse files
add one solution for 1325
1 parent 0d5a21b commit 17a7ebb

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ _If you like this project, please leave me a star._ ★
1515
|1333|[Filter Restaurants by Vegan-Friendly, Price and Distance](https://leetcode.com/problems/filter-restaurants-by-vegan-friendly-price-and-distance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1333.java) | |Medium||
1616
|1331|[Rank Transform of an Array](https://leetcode.com/problems/rank-transform-of-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1331.java) | |Easy||
1717
|1329|[Sort the Matrix Diagonally](https://leetcode.com/problems/sort-the-matrix-diagonally/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1329.java) | |Medium||
18+
|1325|[Delete Leaves With a Given Value](https://leetcode.com/problems/delete-leaves-with-a-given-value/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1325.java) | |Medium|Tree|
1819
|1323|[Maximum 69 Number](https://leetcode.com/problems/maximum-69-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1323.java) | |Easy|Math|
1920
|1317|[Convert Integer to the Sum of Two No-Zero Integers](https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1317.java) | |Easy||
2021
|1315|[Sum of Nodes with Even-Valued Grandparent](https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1315.java) | |Medium|Tree, DFS|

src/main/java/com/fishercoder/solutions/_1325.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,43 @@
6666
* */
6767
public class _1325 {
6868
public static class Solution1 {
69+
/**
70+
* my original but verbose solution
71+
*/
6972
public TreeNode removeLeafNodes(TreeNode root, int target) {
70-
return null;
73+
while (hasTargetLeafNodes(root, target)) {
74+
root = removeLeafNodes(target, root);
75+
}
76+
return root;
77+
}
78+
79+
private boolean hasTargetLeafNodes(TreeNode root, int target) {
80+
if (root == null) {
81+
return false;
82+
}
83+
if (root.left == null && root.right == null && root.val == target) {
84+
return true;
85+
}
86+
return hasTargetLeafNodes(root.left, target) || hasTargetLeafNodes(root.right, target);
87+
}
88+
89+
private TreeNode removeLeafNodes(int target, TreeNode root) {
90+
if (root == null) {
91+
return root;
92+
}
93+
if (root.val == target && root.left == null && root.right == null) {
94+
root = null;
95+
return root;
96+
}
97+
if (root.left != null && root.left.val == target && root.left.left == null && root.left.right == null) {
98+
root.left = null;
99+
}
100+
if (root.right != null && root.right.val == target && root.right.left == null && root.right.right == null) {
101+
root.right = null;
102+
}
103+
removeLeafNodes(target, root.left);
104+
removeLeafNodes(target, root.right);
105+
return root;
71106
}
72107
}
73108
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._1325;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import java.util.Arrays;
10+
11+
import static org.junit.Assert.assertEquals;
12+
13+
public class _1325Test {
14+
private static _1325.Solution1 solution1;
15+
private static TreeNode root;
16+
private static TreeNode expected;
17+
18+
@BeforeClass
19+
public static void setup() {
20+
solution1 = new _1325.Solution1();
21+
}
22+
23+
@Test
24+
public void test1() {
25+
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 2, null, 2, 4));
26+
TreeUtils.printBinaryTree(root);
27+
expected = TreeUtils.constructBinaryTree(Arrays.asList(1, null, 3, null, 4));
28+
TreeUtils.printBinaryTree(expected);
29+
assertEquals(expected, solution1.removeLeafNodes(root, 2));
30+
}
31+
32+
@Test
33+
public void test2() {
34+
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 3, 3, 3, 2));
35+
TreeUtils.printBinaryTree(root);
36+
expected = TreeUtils.constructBinaryTree(Arrays.asList(1, 3, null, null, 2));
37+
TreeUtils.printBinaryTree(expected);
38+
assertEquals(expected, solution1.removeLeafNodes(root, 3));
39+
}
40+
41+
@Test
42+
public void test3() {
43+
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, null, 2, null, 2));
44+
TreeUtils.printBinaryTree(root);
45+
expected = TreeUtils.constructBinaryTree(Arrays.asList(1));
46+
TreeUtils.printBinaryTree(expected);
47+
assertEquals(expected, solution1.removeLeafNodes(root, 2));
48+
}
49+
50+
@Test
51+
public void test4() {
52+
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 1, 1));
53+
TreeUtils.printBinaryTree(root);
54+
expected = null;
55+
TreeUtils.printBinaryTree(expected);
56+
assertEquals(expected, solution1.removeLeafNodes(root, 1));
57+
}
58+
59+
@Test
60+
public void test5() {
61+
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3));
62+
TreeUtils.printBinaryTree(root);
63+
expected = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3));
64+
TreeUtils.printBinaryTree(expected);
65+
assertEquals(expected, solution1.removeLeafNodes(root, 1));
66+
}
67+
68+
}

0 commit comments

Comments
 (0)