-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreeRightSideView.java
More file actions
58 lines (51 loc) · 1.75 KB
/
BinaryTreeRightSideView.java
File metadata and controls
58 lines (51 loc) · 1.75 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package solutions;
import datastructure.TreeNode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
// [Problem] https://leetcode.com/problems/binary-tree-right-side-view
class BinaryTreeRightSideView {
// BFS
// O(n) time, O(d) space where d = tree diameter
public List<Integer> rightSideView(TreeNode root) {
List<Integer> rightmostValues = new ArrayList<>();
if (root == null) {
return rightmostValues;
}
Queue<TreeNode> nodes = new LinkedList<>();
nodes.add(root);
while (!nodes.isEmpty()) {
int currentLevelSize = nodes.size();
for (int i = 0; i < currentLevelSize; i++) {
TreeNode node = nodes.poll();
if (i == currentLevelSize - 1) {
rightmostValues.add(node.val);
}
if (node.left != null) {
nodes.add(node.left);
}
if (node.right != null) {
nodes.add(node.right);
}
}
}
return rightmostValues;
}
// Test
public static void main(String[] args) {
BinaryTreeRightSideView solution = new BinaryTreeRightSideView();
// Given input tree:
// 1
// / \
// 2 3
// \ \
// 5 4
TreeNode root = new TreeNode(1,
new TreeNode(2, null, new TreeNode(5)),
new TreeNode(3, null, new TreeNode(4)));
List<Integer> expectedOutput = List.of(1, 3, 4);
List<Integer> actualOutput = solution.rightSideView(root);
System.out.println("Test passed? " + expectedOutput.equals(actualOutput));
}
}