- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1469.java
32 lines (26 loc) · 893 Bytes
/
_1469.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
packagecom.fishercoder.solutions.secondthousand;
importcom.fishercoder.common.classes.TreeNode;
importjava.util.ArrayList;
importjava.util.List;
publicclass_1469 {
publicstaticclassSolution1 {
publicList<Integer> getLonelyNodes(TreeNoderoot) {
List<Integer> lonelyNodes = newArrayList<>();
dfs(root, lonelyNodes);
returnlonelyNodes;
}
privatevoiddfs(TreeNoderoot, List<Integer> lonelyNodes) {
if (root == null) {
return;
}
if (root.left == null && root.right != null) {
lonelyNodes.add(root.right.val);
}
if (root.left != null && root.right == null) {
lonelyNodes.add(root.left.val);
}
dfs(root.left, lonelyNodes);
dfs(root.right, lonelyNodes);
}
}
}