-
Notifications
You must be signed in to change notification settings - Fork 21.1k
Expand file tree
/
Copy pathRightViewOfBinaryTree.java
More file actions
92 lines (81 loc) · 2.66 KB
/
RightViewOfBinaryTree.java
File metadata and controls
92 lines (81 loc) · 2.66 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.thealgorithms.datastructures.trees;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
/**
* The Right View of a Binary Tree is the set of nodes visible when the tree
* is viewed from the right side. For each level, the rightmost node is part
* of the right view.
*
* <p>This implementation provides both DFS and BFS approaches.</p>
*/
public class RightViewOfBinaryTree {
/**
* Node class structure. If the repository already has a standard Node class,
* reuse it instead of redefining this one.
*/
static class Node {
int data;
Node left;
Node right;
Node(int data) {
this.data = data;
left = right = null;
}
}
/**
* Returns the right view of the binary tree using DFS.
*
* @param root the root of the binary tree
* @return list of node values visible from the right
*/
public static List<Integer> rightViewDFS(Node root) {
List<Integer> result = new ArrayList<>();
dfsHelper(root, 0, result);
return result;
}
private static void dfsHelper(Node node, int level, List<Integer> result) {
if (node == null) return;
if (level == result.size()) {
result.add(node.data);
}
dfsHelper(node.right, level + 1, result);
dfsHelper(node.left, level + 1, result);
}
/**
* Returns the right view using a level-order (BFS) approach.
*
* @param root the root node
* @return list of right view nodes
*/
public static List<Integer> rightViewBFS(Node root) {
List<Integer> result = new ArrayList<>();
if (root == null) return result;
Queue<Node> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
Node current = queue.poll();
if (i == size - 1) {
result.add(current.data);
}
if (current.left != null) queue.offer(current.left);
if (current.right != null) queue.offer(current.right);
}
}
return result;
}
// Example usage
public static void main(String[] args) {
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.right = new Node(6);
System.out.println("Right View (DFS): " + rightViewDFS(root));
System.out.println("Right View (BFS): " + rightViewBFS(root));
}
}