Binary Tree · Views

Left View of Binary Tree

The first node visible at each level when the tree is seen from the left side.

BFS / DFSLevel OrderInterview Essential

Key concepts at a glance — for those who already know the basics.

Problem Statement

Left View of Binary Tree

Given the root of a binary tree, return the values of the nodes visible from the left side, ordered from top to bottom.
Example 1

Input: root = [1, 2, 3, null, 5, null, 4]

Output: [1, 2, 4]

Tree: 1 → left=2, right=3; 2's right=5; 3's right=4.

Example 2

Input: root = [1, 2, 3, 4, 5, 6, 7]

Output: [1, 2, 4]

Perfect binary tree — leftmost nodes at each level.

Constraints

  • The number of nodes in the tree is in the range [0, 100].
  • Each node's value is in the range [-100, 100].
01

The Rule

First node seen at each level from the left

Equivalently: the first node you encounter per depth in a level-order (BFS) traversal.

02

How to Think

1

Process the tree level by level (BFS with a queue)

Each queue iteration handles exactly one full level.

2

Record the very first node dequeued at each level

That first node is the leftmost visible node at that depth.

3

Add left child first, then right child, to the queue

Ensures left nodes always precede right nodes in the queue.

03

Code

solution.py
from collections import deque
 
def left_view(root):
if not root: return []
result, queue = [], deque([root])
while queue:
level_size = len(queue)
for i in range(level_size):
node = queue.popleft()
if i == 0: result.append(node.val)
if node.left: queue.append(node.left)
if node.right: queue.append(node.right)
return result
04

Example

1L02L134L2567Left View → [1, 2, 4]

Blue nodes are the left view: [1, 2, 4]

05

Complexity

Time

O(n)

Every node is enqueued and dequeued exactly once.

Space

O(w)

Queue holds at most the width of the widest level.

06

Interview Uses

Right View of Binary Tree

Same pattern, record the last node per level instead of the first.

Level order traversal

Foundational BFS pattern that left view builds on.

Maximum width of a tree

Same BFS loop, just track max queue size per level.

Vertical order traversal

Another view-based problem sharing similar BFS structure.

Ready to see it in action?

Step through the visualizer to watch the algorithm state update live.

Open Visualizer