Skip to content

Commit a9b1bcb

Browse files
add one more solution for 1325
1 parent 17a7ebb commit a9b1bcb

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

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

+15
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,19 @@ private TreeNode removeLeafNodes(int target, TreeNode root) {
105105
return root;
106106
}
107107
}
108+
109+
public static class Solution2 {
110+
/**A much more concise and efficient solution.*/
111+
public TreeNode removeLeafNodes(TreeNode root, int target) {
112+
if (root == null) {
113+
return root;
114+
}
115+
root.left = removeLeafNodes(root.left, target);
116+
root.right = removeLeafNodes(root.right, target);
117+
if (root.left == null && root.right == null && root.val == target) {
118+
return null;
119+
}
120+
return root;
121+
}
122+
}
108123
}

src/test/java/com/fishercoder/_1325Test.java

+47
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212

1313
public class _1325Test {
1414
private static _1325.Solution1 solution1;
15+
private static _1325.Solution2 solution2;
1516
private static TreeNode root;
1617
private static TreeNode expected;
1718

1819
@BeforeClass
1920
public static void setup() {
2021
solution1 = new _1325.Solution1();
22+
solution2 = new _1325.Solution2();
2123
}
2224

2325
@Test
@@ -65,4 +67,49 @@ public void test5() {
6567
assertEquals(expected, solution1.removeLeafNodes(root, 1));
6668
}
6769

70+
@Test
71+
public void test6() {
72+
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 2, null, 2, 4));
73+
TreeUtils.printBinaryTree(root);
74+
expected = TreeUtils.constructBinaryTree(Arrays.asList(1, null, 3, null, 4));
75+
TreeUtils.printBinaryTree(expected);
76+
assertEquals(expected, solution2.removeLeafNodes(root, 2));
77+
}
78+
79+
@Test
80+
public void test7() {
81+
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 3, 3, 3, 2));
82+
TreeUtils.printBinaryTree(root);
83+
expected = TreeUtils.constructBinaryTree(Arrays.asList(1, 3, null, null, 2));
84+
TreeUtils.printBinaryTree(expected);
85+
assertEquals(expected, solution2.removeLeafNodes(root, 3));
86+
}
87+
88+
@Test
89+
public void test8() {
90+
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, null, 2, null, 2));
91+
TreeUtils.printBinaryTree(root);
92+
expected = TreeUtils.constructBinaryTree(Arrays.asList(1));
93+
TreeUtils.printBinaryTree(expected);
94+
assertEquals(expected, solution2.removeLeafNodes(root, 2));
95+
}
96+
97+
@Test
98+
public void test9() {
99+
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 1, 1));
100+
TreeUtils.printBinaryTree(root);
101+
expected = null;
102+
TreeUtils.printBinaryTree(expected);
103+
assertEquals(expected, solution2.removeLeafNodes(root, 1));
104+
}
105+
106+
@Test
107+
public void test10() {
108+
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3));
109+
TreeUtils.printBinaryTree(root);
110+
expected = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3));
111+
TreeUtils.printBinaryTree(expected);
112+
assertEquals(expected, solution2.removeLeafNodes(root, 1));
113+
}
114+
68115
}

0 commit comments

Comments
 (0)