KatInterview

Collect Binary Tree Node Values in Left-Root-Right Order

Trees & GraphsAuto-graded · 18 test cases
MediumAuto-gradedTrees & Graphsbinary-treetraversalstack

Last updated

Tests binary tree traversal using an explicit stack to visit nodes in left-root-right sequence from a level-order serialized input.

You are given a binary tree represented as a level-order serialized list, where None entries indicate missing children. Your task is to return the values of all nodes visited in left-root-right order (also called in-order traversal).

Level-Order Serialization Format:

The input list contains node values row by row from top to bottom, left to right. Each node in the list must be assigned exactly two child slots (left and right) from subsequent positions in the list, consuming them in order. A None value in a child slot means that position has no node—it is consumed but creates no node, and therefore has no children of its own to place in the list.

For example, [1, None, 2, 3] represents:

  1
   \
    2
   /
  3

Here's how the deserialization works:

  • Index 0: Create node with value 1, enqueue it
  • Process node 1: its left child is index 1 (None, so no node created), its right child is index 2 (create node 2, enqueue it)
  • Process node 2: its left child is index 3 (create node 3, enqueue it), its right child is index 4 (out of bounds, so None)
  • Process node 3: no more indices available, both children are None

Node 3 is the left child of node 2, not a child of the missing left child of node 1, because a None entry creates no node and therefore consumes no child slots from the list.

In-Order Traversal:

For each node, visit all nodes in its left subtree first, then the node itself, then all nodes in its right subtree. This produces a left-root-right ordering.

Your function must:

  1. Reconstruct the tree from the serialized format
  2. Traverse it to collect node values in left-root-right order
  3. Return the collected values as a list

Constraints:

  • 0 ≤ tree.length ≤ 10⁴
  • -1000 ≤ tree[i] ≤ 1000 when tree[i] is not None
  • The input represents a valid binary tree in level-order format
  • Node values may be zero, negative, or duplicated
  • An empty list or [None] represents an empty tree and should return an empty list

Examples

Example 1

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

Output: [1, 3, 2]

Example 2

Input: collect_inorder_values([1, 2, 3, 4, 5, None, 7])

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

Example 3

Input: collect_inorder_values([])

Output: []

Example 4

Input: collect_inorder_values([1])

Output: [1]

Hints

Hint 1

First build the tree by consuming pairs of children for each node in a queue. A None entry is consumed but creates no node to enqueue.

Hint 2

For the traversal, simulate the call stack explicitly: push nodes while going left, pop when you can't go further left, emit the value, then move right.

Hint 3

The loop continues while either the current pointer is non-null or the stack has nodes remaining.