Skip to content

Commit b750dc2

Browse files
committed
Add Right View of Binary Tree algorithm (DFS + BFS)
1 parent 5f8d8ee commit b750dc2

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
* The Right View of a Binary Tree is the set of nodes visible when the tree
10+
* is viewed from the right side. For each level, the rightmost node is part
11+
* of the right view.
12+
*
13+
* <p>This implementation provides both DFS and BFS approaches.</p>
14+
*/
15+
public class RightViewOfBinaryTree {
16+
17+
/**
18+
* Node class structure. If the repository already has a standard Node class,
19+
* reuse it instead of redefining this one.
20+
*/
21+
static class Node {
22+
int data;
23+
Node left;
24+
Node right;
25+
26+
Node(int data) {
27+
this.data = data;
28+
left = right = null;
29+
}
30+
}
31+
32+
/**
33+
* Returns the right view of the binary tree using DFS.
34+
*
35+
* @param root the root of the binary tree
36+
* @return list of node values visible from the right
37+
*/
38+
public static List<Integer> rightViewDFS(Node root) {
39+
List<Integer> result = new ArrayList<>();
40+
dfsHelper(root, 0, result);
41+
return result;
42+
}
43+
44+
private static void dfsHelper(Node node, int level, List<Integer> result) {
45+
if (node == null) return;
46+
if (level == result.size()) {
47+
result.add(node.data);
48+
}
49+
dfsHelper(node.right, level + 1, result);
50+
dfsHelper(node.left, level + 1, result);
51+
}
52+
53+
/**
54+
* Returns the right view using a level-order (BFS) approach.
55+
*
56+
* @param root the root node
57+
* @return list of right view nodes
58+
*/
59+
public static List<Integer> rightViewBFS(Node root) {
60+
List<Integer> result = new ArrayList<>();
61+
if (root == null) return result;
62+
63+
Queue<Node> queue = new LinkedList<>();
64+
queue.offer(root);
65+
66+
while (!queue.isEmpty()) {
67+
int size = queue.size();
68+
for (int i = 0; i < size; i++) {
69+
Node current = queue.poll();
70+
if (i == size - 1) {
71+
result.add(current.data);
72+
}
73+
if (current.left != null) queue.offer(current.left);
74+
if (current.right != null) queue.offer(current.right);
75+
}
76+
}
77+
return result;
78+
}
79+
80+
// Example usage
81+
public static void main(String[] args) {
82+
Node root = new Node(1);
83+
root.left = new Node(2);
84+
root.right = new Node(3);
85+
root.left.left = new Node(4);
86+
root.left.right = new Node(5);
87+
root.right.right = new Node(6);
88+
89+
System.out.println("Right View (DFS): " + rightViewDFS(root));
90+
System.out.println("Right View (BFS): " + rightViewBFS(root));
91+
}
92+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.thealgorithms.datastructures.trees;
2+
3+
import org.junit.jupiter.api.Test;
4+
import java.util.List;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
public class RightViewOfBinaryTreeTest {
8+
9+
@Test
10+
public void testRightViewOfBalancedTree() {
11+
RightViewOfBinaryTree.Node root = new RightViewOfBinaryTree.Node(1);
12+
root.left = new RightViewOfBinaryTree.Node(2);
13+
root.right = new RightViewOfBinaryTree.Node(3);
14+
root.left.left = new RightViewOfBinaryTree.Node(4);
15+
root.left.right = new RightViewOfBinaryTree.Node(5);
16+
root.right.right = new RightViewOfBinaryTree.Node(6);
17+
18+
List<Integer> expected = List.of(1, 3, 6);
19+
assertEquals(expected, RightViewOfBinaryTree.rightViewDFS(root));
20+
}
21+
22+
@Test
23+
public void testRightSkewedTree() {
24+
RightViewOfBinaryTree.Node root = new RightViewOfBinaryTree.Node(1);
25+
root.right = new RightViewOfBinaryTree.Node(2);
26+
root.right.right = new RightViewOfBinaryTree.Node(3);
27+
28+
List<Integer> expected = List.of(1, 2, 3);
29+
assertEquals(expected, RightViewOfBinaryTree.rightViewDFS(root));
30+
}
31+
32+
@Test
33+
public void testLeftSkewedTree() {
34+
RightViewOfBinaryTree.Node root = new RightViewOfBinaryTree.Node(1);
35+
root.left = new RightViewOfBinaryTree.Node(2);
36+
root.left.left = new RightViewOfBinaryTree.Node(3);
37+
38+
List<Integer> expected = List.of(1, 2, 3);
39+
assertEquals(expected, RightViewOfBinaryTree.rightViewBFS(root));
40+
}
41+
}

0 commit comments

Comments
 (0)