KatInterview

Column-by-Column Readout of a Binary Tree from Level-Order Input

Trees & GraphsAuto-graded · 23 test cases
MediumAuto-gradedTrees & Graphsbinary-treebfssorting

Last updated

Group binary tree nodes by horizontal column, ordering each column by depth and then by value, with the tree given as a level-order list with nulls.

Column-by-Column Readout of a Binary Tree

You are given a binary tree encoded as a list in level-order, where None marks a missing child. Return the node values grouped by the vertical column each node sits in, from the leftmost column to the rightmost.

Reading the input

The list is read left to right, one level at a time:

  • level_order[0] is the root.
  • Each real (non-None) node, in the order it is discovered, consumes the next two unread entries of the list. The first is its left child and the second is its right child. None means that child does not exist.
  • A None entry has no children, so it consumes nothing further.
  • If the list ends early, all remaining children are absent.

For example, [1, 2, 3, None, 4, None, 5] describes a root 1 with children 2 and 3. Node 2 has no left child and a right child 4. Node 3 has no left child and a right child 5.

Coordinates

Assign each node a position (row, col):

  • The root is at (0, 0).
  • A left child of a node at (r, c) is at (r + 1, c - 1).
  • A right child of a node at (r, c) is at (r + 1, c + 1).

Output

Return a list of lists. Each inner list holds the values of one column, and the inner lists are ordered by increasing col. Within a column, order the values by:

  1. Increasing row (shallower nodes first).
  2. If two nodes share the same row and the same col, increasing node value.

Only columns that contain at least one node appear in the output.

If the tree is empty (the list is empty, or its first entry is None), return an empty list.

Constraints

  • The list holds between 0 and about 1000 entries.
  • Node values are integers. Duplicate values are allowed.
  • The input follows the encoding described above, and trailing None entries may be omitted.
  • The output must match exactly, including the order within each column.

Examples

Example 1

Input: vertical_slices([3, 9, 20, None, None, 15, 7])

Output: [[9], [3, 15], [20], [7]]

Example 2

Input: vertical_slices([1])

Output: [[1]]

Example 3

Input: vertical_slices([1, 2, 3, 4, 5, 6, 7])

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

Example 4

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

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

Hints

Hint 1

Think of each node as a triple (column, row, value). If you sort the triples in that order, the required output ordering falls out directly.

Hint 2

Decode the level-order list with a queue: each node popped takes the next two list entries as its children, and you pass down row + 1 and col -/+ 1 as you push them.

Hint 3

After sorting, walk through the triples and start a new inner list every time the column changes. The traversal order does not matter because the sort decides the final order.