KatInterview

Column Grid from a Tree: Reach a Value by Walking Non-Zero Cells

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

Last updated

Project a heap-indexed binary tree onto a depth-by-column grid, then use BFS over non-zero cells to decide whether a cell holding the target is reachable from a given start.

Tree on a Grid

A binary tree is given as a level-order list using heap indexing. The node at index i has its left child at index 2i + 1 and its right child at index 2i + 2. A None entry means that position has no node. Children of a missing node are never part of the tree, even if the list has non-None values at those indices.

Step 1: assign coordinates

Each node gets a depth and a column:

  • The root has depth 0 and column 0.
  • A left child has its parent's depth plus 1 and its parent's column minus 1.
  • A right child has its parent's depth plus 1 and its parent's column plus 1.

Step 2: build the grid

Build a 2D grid with:

  • max_depth + 1 rows, where max_depth is the largest depth of any node.
  • One column for each column index from the smallest to the largest used by any node. Shift the indices so the smallest column becomes grid column 0.

Every cell starts as 0. Then write each node's value into the cell at (depth, column - min_column).

Two different nodes can land in the same cell. Process nodes in increasing index order (level order), so the node that comes later in the list overwrites the earlier one.

Step 3: walk the grid

You are given a starting cell (start_row, start_col), expressed in the shifted grid coordinates, and an integer target. From a cell you may move up, down, left, or right, but only into cells whose value is non-zero.

Return True if some cell holding target can be reached from the start, including the start cell itself (zero moves). Return False otherwise.

Special cases

  • If the list is empty or its first element is None, return False.
  • If the start cell is outside the grid, return False.
  • If the start cell holds 0, return False.
  • The target is never reachable if it is 0 or does not appear in the grid.

Function

Implement grid_reach(tree, start_row, start_col, target) returning a boolean.

Constraints

  • The tree has at most 10^4 nodes.
  • Every node value is a non-zero integer.
  • start_row, start_col, and target are integers. The start may lie outside the grid.
  • Grid dimensions come entirely from the tree's shape as described above.

Examples

Example 1

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

Output: False

Example 2

Input: grid_reach([1], 0, 0, 1)

Output: True

Example 3

Input: grid_reach([1, 2, 3], 0, 0, 9)

Output: False

Hints

Hint 1

First turn the list into (depth, column, value) triples. A BFS from index 0 that only enqueues non-None children handles missing subtrees automatically.

Hint 2

Compute the minimum and maximum column and the maximum depth, allocate the grid, and fill it in the order the nodes were discovered so later nodes overwrite earlier ones.

Hint 3

Check the start cell for bounds and for zero before searching, then run a standard 4-direction BFS over non-zero cells, testing each dequeued cell against the target.