Skip to content

Commit 5bb0c04

Browse files
add 1469
1 parent 75e6d34 commit 5bb0c04

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ _If you like this project, please leave me a star._ ★
2626
|1475|[Final Prices With a Special Discount in a Shop](https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1475.java) | |Easy|Array|
2727
|1471|[The k Strongest Values in an Array](https://leetcode.com/problems/the-k-strongest-values-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1471.java) | |Medium|Array, Sort|
2828
|1470|[Shuffle the Array](https://leetcode.com/problems/shuffle-the-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1470.java), [C++](../master/cpp/_1470.cpp) | |Easy|Array|
29+
|1469|[Find All The Lonely Nodes](https://leetcode.com/problems/find-all-the-lonely-nodes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1469.java) | |Easy|Tree, DFS|
2930
|1466|[Reorder Routes to Make All Paths Lead to the City Zero](https://leetcode.com/problems/reorder-routes-to-make-all-paths-lead-to-the-city-zero/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1466.java) | |Medium|Tree, DFS|
3031
|1464|[Maximum Product of Two Elements in an Array](https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1464.java) | |Easy|Array|
3132
|1460|[Make Two Arrays Equal by Reversing Sub-arrays](https://leetcode.com/problems/make-two-arrays-equal-by-reversing-sub-arrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1460.java) | |Easy|Array|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class _1469 {
9+
public static class Solution1 {
10+
public List<Integer> getLonelyNodes(TreeNode root) {
11+
List<Integer> lonelyNodes = new ArrayList<>();
12+
dfs(root, lonelyNodes);
13+
return lonelyNodes;
14+
}
15+
16+
private void dfs(TreeNode root, List<Integer> lonelyNodes) {
17+
if (root == null) {
18+
return;
19+
}
20+
21+
if (root.left == null && root.right != null) {
22+
lonelyNodes.add(root.right.val);
23+
}
24+
25+
if (root.left != null && root.right == null) {
26+
lonelyNodes.add(root.left.val);
27+
}
28+
29+
dfs(root.left, lonelyNodes);
30+
dfs(root.right, lonelyNodes);
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.TreeUtils;
4+
import com.fishercoder.solutions._1469;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import java.util.Arrays;
9+
10+
import static junit.framework.TestCase.assertEquals;
11+
12+
public class _1469Test {
13+
private static _1469.Solution1 solution1;
14+
15+
@BeforeClass
16+
public static void setup() {
17+
solution1 = new _1469.Solution1();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
assertEquals(Arrays.asList(4), solution1.getLonelyNodes(TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, null, 4))));
23+
}
24+
25+
@Test
26+
public void test2() {
27+
assertEquals(Arrays.asList(6, 2), solution1.getLonelyNodes(TreeUtils.constructBinaryTree(Arrays.asList(7, 1, 4, 6, null, 5, 3, null, null, null, null, null, 2))));
28+
}
29+
30+
}

0 commit comments

Comments
 (0)