Binary trees are one of the most fundamental data structures in computer science. Beyond basic traversals like inorder, preorder, and postorder, interviewers often test advanced problems such as:
- Left View
- Right View
- Top View
- Bottom View
- Level Order Traversal
- Zigzag Traversal
In this blog, weβll implement each of these with clean, modular Java code.
Binary Tree Node Definition
Weβll start with a simple TreeNode
class:
class TreeNode {
int val;
TreeNode left, right;
TreeNode(int val) {
this.val = val;
left = right = null;
}
}
1. Left View of a Binary Tree
π The left view contains the first node visible at each level when the tree is viewed from the left.
Code:
import java.util.*;
public class BinaryTreeViews {
// Left View
public List<Integer> leftView(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null) return result;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
if (i == 0) result.add(node.val); // First node of each level
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
}
return result;
}
}
2. Right View of a Binary Tree
π The right view contains the last node visible at each level when viewed from the right.
Code:
// Right View
public List<Integer> rightView(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null) return result;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
if (i == size - 1) result.add(node.val); // Last node of each level
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
}
return result;
}
3. Top View of a Binary Tree
π The top view shows nodes visible from above, i.e., the first node encountered at each horizontal distance.
We track nodes using horizontal distance (HD) with a TreeMap
.
Code:
// Top View
public List<Integer> topView(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null) return result;
class Pair {
TreeNode node; int hd;
Pair(TreeNode n, int h) { node = n; hd = h; }
}
Map<Integer, Integer> map = new TreeMap<>();
Queue<Pair> queue = new LinkedList<>();
queue.offer(new Pair(root, 0));
while (!queue.isEmpty()) {
Pair p = queue.poll();
if (!map.containsKey(p.hd)) map.put(p.hd, p.node.val);
if (p.node.left != null) queue.offer(new Pair(p.node.left, p.hd - 1));
if (p.node.right != null) queue.offer(new Pair(p.node.right, p.hd + 1));
}
result.addAll(map.values());
return result;
}
4. Bottom View of a Binary Tree
π The bottom view shows the last node at each horizontal distance (overwriting previous ones).
Code:
// Bottom View
public List<Integer> bottomView(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null) return result;
class Pair {
TreeNode node; int hd;
Pair(TreeNode n, int h) { node = n; hd = h; }
}
Map<Integer, Integer> map = new TreeMap<>();
Queue<Pair> queue = new LinkedList<>();
queue.offer(new Pair(root, 0));
while (!queue.isEmpty()) {
Pair p = queue.poll();
map.put(p.hd, p.node.val); // overwrite to get bottom-most
if (p.node.left != null) queue.offer(new Pair(p.node.left, p.hd - 1));
if (p.node.right != null) queue.offer(new Pair(p.node.right, p.hd + 1));
}
result.addAll(map.values());
return result;
}
5. Level Order Traversal
π Classic Breadth-First Traversal (BFS).
Code:
// Level Order Traversal
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();
if (root == null) return result;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
List<Integer> level = new ArrayList<>();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
level.add(node.val);
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
result.add(level);
}
return result;
}
6. Zigzag Traversal
π Like level order, but alternate levels go left-to-right, then right-to-left.
Code:
// Zigzag Traversal
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();
if (root == null) return result;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
boolean leftToRight = true;
while (!queue.isEmpty()) {
int size = queue.size();
LinkedList<Integer> level = new LinkedList<>();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
if (leftToRight) level.addLast(node.val);
else level.addFirst(node.val);
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
result.add(level);
leftToRight = !leftToRight;
}
return result;
}
}
Example Usage
public class Main {
public static void main(String[] args) {
BinaryTreeViews bt = new BinaryTreeViews();
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
root.right.right = new TreeNode(6);
root.right.right.left = new TreeNode(7);
System.out.println("Left View: " + bt.leftView(root));
System.out.println("Right View: " + bt.rightView(root));
System.out.println("Top View: " + bt.topView(root));
System.out.println("Bottom View: " + bt.bottomView(root));
System.out.println("Level Order: " + bt.levelOrder(root));
System.out.println("Zigzag Traversal: " + bt.zigzagLevelOrder(root));
}
}
Output (for above tree)
Left View: [1, 2, 4, 7]
Right View: [1, 3, 6, 7]
Top View: [4, 2, 1, 3, 6]
Bottom View: [4, 2, 5, 7, 6]
Level Order: [[1], [2, 3], [4, 5, 6], [7]]
Zigzag Traversal: [[1], [3, 2], [4, 5, 6], [7]]
π Conclusion
These binary tree problems are staples in coding interviews and help you master BFS, DFS, and horizontal distance mapping techniques.
- Left & Right View β BFS with first/last node logic
-
Top & Bottom View β BFS with horizontal distance +
TreeMap
- Level Order & Zigzag β Variants of BFS
By practicing these, youβll gain confidence in solving tree-based problems under pressure.
Top comments (0)