Left View of Binary Tree

easy

Given the root of a binary tree, imagine standing on its left side. Return the values of the nodes you can see, ordered from top to bottom — that is, the leftmost node at each level.

The tree is given in level-order (breadth-first), using null for missing children.

Hints

The visible node at each level is the leftmost one.
In level-order BFS that's simply the first node dequeued on each level.
A DFS that goes left before right records the first node it reaches at each new depth.

Common doubts

No — if a level's leftmost node lacks a left child, the visible node can descend from a right child; it's the leftmost node, not the left child.
Going left before right, the first node reached at any depth is the leftmost at that depth, so you record on first arrival.
The mirror: the last node of each level in BFS, or a right-first DFS.

Interview follow-ups

Take the last node of each level in BFS, or recurse right-before-left and record the first node at each depth.
Group nodes by horizontal distance instead of level, keeping the first (top) or last (bottom) node per column.

Fun facts

  • The left-side and right-side views are exact mirrors: swap 'first' and 'last' of each level.
  • Left-first DFS computes the view with O(height) space instead of O(width).

Asked at

AmazonMicrosoftPaytm
Frequently Sometimes Occasionally
Example 1
Input: root = [1, 2, 3, 4, 5, null, null, null, 6]
Output: [1, 2, 4, 6]
From the left you see the leftmost node at each of the four levels.
Example 2
Input: root = [1, null, 3]
Output: [1, 3]
The 3 is the leftmost (only) node on level 1, even though it's a right child.
Example 3
Input: root = []
Output: []
An empty tree shows nothing.
Constraints

- The number of nodes is in the range [0, 100] - 1 <= Node.val <= 10^5

Solve this problem →