Skip to content

Commit 402d3b0

Browse files
feat: add RightSideViewOfBinaryTree algorithm with tests (#7734)
* feat: add RightSideViewOfBinaryTree algorithm with tests * fix: correct input array for testRightSkewedTree
1 parent 2f04e81 commit 402d3b0

2 files changed

Lines changed: 116 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.thealgorithms.datastructures.trees;
2+
3+
import java.util.ArrayList;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.Queue;
7+
8+
/**
9+
* Given the root of a binary tree, returns the values of the nodes visible
10+
* from the right side (the last node of each level in a level-order traversal).
11+
*
12+
* <p>Example:
13+
* <pre>
14+
* 1
15+
* / \
16+
* 2 3
17+
* \ \
18+
* 5 4
19+
* Right side view: [1, 3, 4]
20+
* </pre>
21+
*
22+
* <p>Time complexity: O(n), Space complexity: O(n)
23+
*
24+
* @see <a href="https://leetcode.com/problems/binary-tree-right-side-view/">LeetCode #199</a>
25+
*/
26+
public final class RightSideViewOfBinaryTree {
27+
private RightSideViewOfBinaryTree() {
28+
}
29+
30+
public static List<Integer> rightSideView(BinaryTree.Node root) {
31+
List<Integer> result = new ArrayList<>();
32+
if (root == null) {
33+
return result;
34+
}
35+
36+
Queue<BinaryTree.Node> queue = new LinkedList<>();
37+
queue.add(root);
38+
39+
while (!queue.isEmpty()) {
40+
int levelSize = queue.size();
41+
for (int i = 0; i < levelSize; i++) {
42+
BinaryTree.Node node = queue.poll();
43+
if (i == levelSize - 1) {
44+
result.add(node.data);
45+
}
46+
if (node.left != null) {
47+
queue.add(node.left);
48+
}
49+
if (node.right != null) {
50+
queue.add(node.right);
51+
}
52+
}
53+
}
54+
return result;
55+
}
56+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.thealgorithms.datastructures.trees;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.Collections;
6+
import java.util.List;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class RightSideViewOfBinaryTreeTest {
10+
11+
@Test
12+
public void testNullRoot() {
13+
assertEquals(Collections.emptyList(), RightSideViewOfBinaryTree.rightSideView(null));
14+
}
15+
16+
@Test
17+
public void testSingleNode() {
18+
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[] {1});
19+
assertEquals(List.of(1), RightSideViewOfBinaryTree.rightSideView(root));
20+
}
21+
22+
/*
23+
1
24+
/ \
25+
2 3
26+
\ \
27+
5 4
28+
*/
29+
@Test
30+
public void testRightSideView() {
31+
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[] {1, 2, 3, null, 5, null, 4});
32+
assertEquals(List.of(1, 3, 4), RightSideViewOfBinaryTree.rightSideView(root));
33+
}
34+
35+
/*
36+
1
37+
/
38+
2
39+
/
40+
3
41+
*/
42+
@Test
43+
public void testLeftSkewedTree() {
44+
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[] {1, 2, null, 3});
45+
assertEquals(List.of(1, 2, 3), RightSideViewOfBinaryTree.rightSideView(root));
46+
}
47+
48+
/*
49+
1
50+
\
51+
2
52+
\
53+
3
54+
*/
55+
@Test
56+
public void testRightSkewedTree() {
57+
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[] {1, null, 2, null, 3});
58+
assertEquals(List.of(1, 2, 3), RightSideViewOfBinaryTree.rightSideView(root));
59+
}
60+
}

0 commit comments

Comments
 (0)