Java Binary Tree Program · OT49
Binary Tree Right Side View in Java
From the right side you see exactly one node per level: the rightmost one. A level-by-level traversal finds it directly.
Problem Statement
Return the values of the nodes you would see standing to the right of a binary tree, from top to bottom.
That is the last node of every level, even when it lies in the left subtree because the right side is shorter.
Input/Output Examples
[8, 3, 10, 1, 6, null, 14, null, null, 4, 7]Output[8, 10, 14, 7]Explanation: 7 is visible because nothing to its right is at that depth.[1, 2, null, 3]Output[1, 2, 3]Explanation: every node is the only one on its level.[]Output[]Explanation: nothing to see.Visual Explanation
- Level 0nodes8last = 8
- Level 1nodes310last = 10
- Level 2nodes1614last = 14
- Level 3nodes47last = 7
- Done[8, 10, 14, 7]
[8, 3, 10, 1, 6, null, 14, null, null, 4, 7], each read left to right: the last node of each level (purple) is the one seen from the right.The deepest level contains only 4 and 7, both in the left subtree, so 7 is visible even though it is not on the right branch.
Approaches
Both approaches pick one node per depth. BFS takes the last of each level; DFS visits the right side first.
Approach 1: Level-Order BFS (Recommended)
Idea: process the tree level by level and record the last node of each level.
- Put the root in a queue.
- For each level, process exactly
sizenodes. - Record the node at index
size - 1; enqueue children left then right.
Time: O(n) · Space: O(w). w is the widest level.
Approach 2: Right-First DFS
Idea: visit the right child before the left; the first node reached at each depth is the visible one.
- Recurse with the current depth.
- If
depth == view.size(), this is the first node at this depth: add it. - Recurse right, then left.
Time: O(n) · Space: O(h).
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: Level-Order BFS (Recommended)
The level boundary comes from reading queue.size() before processing the level.
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
public class RightViewBfs {
static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int val) { this.val = val; }
}
// Builds a tree from level-order values, where null means "no child here"
static TreeNode build(Integer... values) {
if (values.length == 0 || values[0] == null) {
return null;
}
TreeNode root = new TreeNode(values[0]);
java.util.Queue<TreeNode> queue = new java.util.ArrayDeque<>();
queue.add(root);
int i = 1;
while (!queue.isEmpty() && i < values.length) {
TreeNode node = queue.poll();
if (i < values.length && values[i] != null) {
node.left = new TreeNode(values[i]);
queue.add(node.left);
}
i++;
if (i < values.length && values[i] != null) {
node.right = new TreeNode(values[i]);
queue.add(node.right);
}
i++;
}
return root;
}
// The last node of each level, read left to right, is the one seen from the right
static List<Integer> rightView(TreeNode root) {
List<Integer> view = new ArrayList<>();
if (root == null) {
return view;
}
Queue<TreeNode> queue = new ArrayDeque<>();
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
if (i == size - 1) {
view.add(node.val);
}
if (node.left != null) queue.add(node.left);
if (node.right != null) queue.add(node.right);
}
}
return view;
}
public static void main(String[] args) {
System.out.println("[8, 3, 10, 1, 6, null, 14, null, null, 4, 7] -> "
+ rightView(build(8, 3, 10, 1, 6, null, 14, null, null, 4, 7)));
System.out.println("[1, 2, null, 3] -> " + rightView(build(1, 2, null, 3)));
System.out.println("[] -> " + rightView(build()));
}
}
[8, 3, 10, 1, 6, null, 14, null, null, 4, 7] -> [8, 10, 14, 7] [1, 2, null, 3] -> [1, 2, 3] [] -> []
Time complexity: O(n) · Space complexity: O(w).
Approach 2: Right-First DFS
Changing the order to left-first gives the left side view instead.
import java.util.ArrayList;
import java.util.List;
public class RightViewDfs {
static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int val) { this.val = val; }
}
// Builds a tree from level-order values, where null means "no child here"
static TreeNode build(Integer... values) {
if (values.length == 0 || values[0] == null) {
return null;
}
TreeNode root = new TreeNode(values[0]);
java.util.Queue<TreeNode> queue = new java.util.ArrayDeque<>();
queue.add(root);
int i = 1;
while (!queue.isEmpty() && i < values.length) {
TreeNode node = queue.poll();
if (i < values.length && values[i] != null) {
node.left = new TreeNode(values[i]);
queue.add(node.left);
}
i++;
if (i < values.length && values[i] != null) {
node.right = new TreeNode(values[i]);
queue.add(node.right);
}
i++;
}
return root;
}
// Visit right before left; the first node reached at each depth is the visible one
static List<Integer> rightView(TreeNode root) {
List<Integer> view = new ArrayList<>();
dfs(root, 0, view);
return view;
}
private static void dfs(TreeNode node, int depth, List<Integer> view) {
if (node == null) {
return;
}
if (depth == view.size()) {
view.add(node.val); // first visit at this depth
}
dfs(node.right, depth + 1, view);
dfs(node.left, depth + 1, view);
}
public static void main(String[] args) {
System.out.println("[8, 3, 10, 1, 6, null, 14, null, null, 4, 7] -> "
+ rightView(build(8, 3, 10, 1, 6, null, 14, null, null, 4, 7)));
System.out.println("[1, 2, null, 3] -> " + rightView(build(1, 2, null, 3)));
System.out.println("[] -> " + rightView(build()));
}
}
[8, 3, 10, 1, 6, null, 14, null, null, 4, 7] -> [8, 10, 14, 7] [1, 2, null, 3] -> [1, 2, 3] [] -> []
Time complexity: O(n) · Space complexity: O(h).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Level-Order BFS | O(n) | O(w) |
| Right-First DFS | O(n) | O(h) |
Here n is the number of nodes. Both approaches visit every node once; BFS stores one level at a time, DFS one root-to-leaf path.
Interview Tips and Recommended Approach
- Do not follow only right pointers. Deeper left nodes can be visible, as 7 is here.
- Read the level size first. The queue grows while the level is being processed.
- Know the variants: left view, top view and bottom view use similar ideas.
- Choose by shape. BFS suits wide, shallow trees; DFS suits narrow, deep ones.
Recommended Approach: Level-Order BFS
Why: it maps the definition, "one node per level", directly onto a level-order traversal.