KatInterview

Left and Right View of a Binary Tree Using BFS and DFS

Trees & GraphsAuto-graded · 19 test cases
MediumAuto-gradedTrees & Graphsbinary-treebfsdfs

Last updated

Tests understanding of binary tree traversal by computing boundary nodes at each depth using both breadth-first and depth-first strategies.

You are given a binary tree encoded as a level-order list where null marks missing children. Your task is to extract two views of the tree:

  1. Left view: For each depth, the leftmost node at that depth.
  2. Right view: For each depth, the rightmost node at that depth.

Both views are lists indexed by depth, starting from depth 0 (the root).

Your solution must compute both views using two different methods—a breadth-first search (BFS) and a depth-first search (DFS)—then verify that both methods produce identical results.

Input Format

The input follows standard level-order encoding:

  • level_order[0] is the root.
  • For any node at list index i, its left child appears at index 2*i+1 and its right child at index 2*i+2, provided those indices exist in the list.
  • A null entry means no node exists at that position but still occupies an index slot.

For example, [1, 2, null, 3, null, 4] means:

  • Root is 1
  • Left child of 1 is 2, right child of 1 is null (missing)
  • Left child of 2 is 3, right child of 2 is null
  • Left child of 3 is 4

This produces a left-skewed tree: 1 → 2 → 3 → 4.

Output Format

Return [left_view, right_view] where:

  • left_view[d] is the value of the leftmost node at depth d.
  • right_view[d] is the value of the rightmost node at depth d.

If the input is empty or the root is null, return [[], []].

Key Rules

  • Leftmost/rightmost means spatial position within the level, not "follow left/right pointers." For example, in the tree [1, 2, 3, 4], node 4 hangs off the left subtree but is still the rightmost node at depth 2 because it is the only node at that depth.
  • When a level has only one node, that node appears in both views.
  • Both BFS and DFS must produce the same results; your function should assert this before returning.

Constraints

  • 0 <= len(level_order) <= 10^5
  • Node values are integers (may be negative, zero, or positive).
  • The tree may be empty, skewed, balanced, or irregular.
  • You must implement both BFS and DFS traversals and verify they match.

Examples

Example 1

Input: compute_tree_views([1, 2, 3, 4, 5, None, 6])

Output: [[1, 2, 4], [1, 3, 6]]

Example 2

Input: compute_tree_views([1])

Output: [[1], [1]]

Example 3

Input: compute_tree_views([1, 2, None, 3, None, 4])

Output: [[1, 2, 3, 4], [1, 2, 3, 4]]

Example 4

Input: compute_tree_views([1, None, 2, None, 3])

Output: [[1, 2, 3], [1, 2, 3]]

Hints

Hint 1

For BFS, snapshot the queue size before processing each level—the first and last nodes dequeued in that batch define the left and right boundaries.

Hint 2

For DFS, carry a depth parameter and append a node's value only when depth equals the current length of the view list, ensuring you capture the first node reached at each depth.

Hint 3

Reconstruct the tree using a queue: dequeue a parent, then read the next two list slots as its children, enqueuing only non-null children.