Definition
- Left view – the mirror of the right view: the first node of each level, top to bottom.
- Level-size snapshot – capture
queue.size()at each level's start, drain that many nodes, and record the first (index == 0). - Not the left spine – the leftmost node per level may come from the right subtree if the left subtree is shorter.
- Complexity – O(n) time, O(w) space for BFS; O(n) time, O(h) space for DFS.
- Symmetric – the right view captures the last node per level.
Looking at the National Bank of India approval tree from the left edge, you see only the leftmost approver on each rank, because they stand in front of everyone to their right. Collecting the leftmost approver per rank top to bottom is the left view.
Understanding the Concepts
What is the left view of a binary tree?
The left view is what you would see standing on the left side of the tree: one node per level, the leftmost. It is the mirror of the right view. For each level, only the first node processed in a BFS is visible.
package com.bank.model;
public class RankNode {
public String name;
public RankNode left;
public RankNode right;
public RankNode(String name) {
this.name = name;
}
}
How do you compute the left view with BFS?
The same BFS level-size snapshot as the right view, but capture the node at index 0 instead of levelSize - 1.
package com.bank.service;
import com.bank.model.RankNode;
import java.util.*;
public class LeftViewBFS {
public static List<String> leftView(RankNode root) {
List<String> result = new ArrayList<>();
if (root == null) return result;
Queue<RankNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int levelSize = queue.size();
for (int i = 0; i < levelSize; i++) {
RankNode node = queue.poll();
if (i == 0) {
result.add(node.name); // first of this level
}
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
}
return result;
}
public static void main(String[] args) {
// CEO
// / \
// VP1 VP2
// \
// DIR2
RankNode ceo = new RankNode("CEO");
ceo.left = new RankNode("VP1");
ceo.right = new RankNode("VP2");
ceo.right.right = new RankNode("DIR2");
System.out.println("Left view: " + leftView(ceo));
}
}
Left view: [CEO, VP1, DIR2]
Why is the left view not just the left spine?
If the right subtree runs deeper than the left, the first node on a deeper level comes from the right side. Following only left children misses it.
// WRONG: following left children only
public static List<String> naiveLeftView(RankNode root) {
List<String> result = new ArrayList<>();
while (root != null) {
result.add(root.name);
root = root.left; // misses right-side deeper nodes!
}
return result;
}
// Returns [CEO, VP1] but misses DIR2 at level 2
How does the DFS alternative work?
Visit left child before right, carrying depth. Record a node when its depth equals the current result size, meaning it is the first node seen at that depth.
public static List<String> leftViewDFS(RankNode root) {
List<String> result = new ArrayList<>();
dfs(root, 0, result);
return result;
}
private static void dfs(RankNode node, int depth, List<String> result) {
if (node == null) return;
if (depth == result.size()) result.add(node.name);
dfs(node.left, depth + 1, result); // left first for left view
dfs(node.right, depth + 1, result);
}
How are left and right views related?
They are mirrors of the same BFS pattern. The only difference is the capture condition:
| View | Capture condition |
|---|---|
| Right view | i == levelSize - 1 (last of level) |
| Left view | i == 0 (first of level) |
// Both views share this skeleton
while (!queue.isEmpty()) {
int levelSize = queue.size();
for (int i = 0; i < levelSize; i++) {
RankNode node = queue.poll();
// Right view: if (i == levelSize - 1) result.add(node);
// Left view: if (i == 0) result.add(node);
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
}
What are the edge cases?
Empty tree, single node, and a tree where every level has only right children.
package com.bank.service;
import com.bank.model.RankNode;
public class LeftViewEdgeCases {
public static void main(String[] args) {
System.out.println("Empty: " + LeftViewBFS.leftView(null));
System.out.println("Single: " + LeftViewBFS.leftView(new RankNode("CEO")));
RankNode rightOnly = new RankNode("CEO");
rightOnly.right = new RankNode("VP");
rightOnly.right.right = new RankNode("DIR");
System.out.println("Right-only: " + LeftViewBFS.leftView(rightOnly));
}
}
Empty: []
Single: [CEO]
Right-only: [CEO, VP, DIR]
What is the complexity?
BFS visits each node once. Time is O(n), space is O(w) for the queue. DFS uses O(h) recursion stack space.
What should a senior engineer know?
Presenting the left and right views together as the symmetric first-node and last-node rules within the same BFS pattern shows the interviewer you see the whole Tree BFS family as one coherent pattern.
In production at National Bank of India, the left view produces an entry-point summary showing the first approver reachable at each escalation rank.
// Left view for entry-point summary
List<String> entryApprovers = LeftViewBFS.leftView(approvalTree);
for (int rank = 0; rank < entryApprovers.size(); rank++) {
System.out.println("Rank " + rank + " entry: " + entryApprovers.get(rank));
}
Visual Overview
BFS Left View
CEO Level 0: first = CEO
/ \
VP1 VP2 Level 1: first = VP1
\
DIR2 Level 2: first = DIR2
Left view: [CEO, VP1, DIR2]
Note: DIR2 is a right child but leftmost at level 2
DFS Left-First Visit Order Visit: CEO(0) → VP1(1) → VP2(1,skip) → DIR2(2) Result builds: [CEO] → [CEO,VP1] → [CEO,VP1,DIR2]
Key Takeaway
The left view of a binary tree is the mirror of the right view: it is the first node of each level, top to bottom. You compute it with the same BFS level-size snapshot, but capture the node at index 0 instead of levelSize - 1. Children are enqueued left then right as in every BFS variant.
The key insight is the same as for the right view: the left view is defined per level, not per branch. Following only left children fails when the right subtree runs deeper. The first-index rule captures the correct node regardless of which subtree it comes from.
The DFS alternative visits left before right and records the first node at each new depth. Both BFS and DFS are O(n) time; BFS uses O(w) space and DFS uses O(h) space.
In our banking system at National Bank of India, the left view produces an entry-point summary showing the first approver reachable at each escalation rank. Presenting the left and right views together as the symmetric first-node and last-node rules shows command of the whole Tree BFS family.
Remember one sentence: BFS level-size snapshot, capture the first node per level; that is the left view, the mirror of the right view.
---