Tallest Chain of Nodes in an Index-Linked Binary Tree
Trees & GraphsAuto-graded · 17 test casesLast updated
Compute the height of a binary tree given as a child-index table, handling empty input and very deep skewed trees without recursion failures.
Tallest Chain of Nodes in an Index-Linked Binary Tree
You are given a binary tree stored as a table of child indices. Your job is to report its height, measured as the number of nodes on the longest downward path that starts at the root and ends at a leaf.
Input format
The argument child_table is a list of [left, right] pairs.
- Nodes are numbered
0ton - 1, wherenis the length of the list. - Row
iholds the identifiers of nodei's left child and right child. - The value
-1means that the child does not exist. - When the list is not empty, node
0is the root.
Task
Implement tree_height(child_table) and return a single integer: the count of nodes along the longest path from the root down to a leaf.
- An empty list describes an empty tree, and the answer is
0. - A tree consisting of just the root has answer
1.
Constraints
0 <= n <= 200000- The table always describes one valid binary tree: every node can be reached from node
0, there are no cycles, and every node other than the root has exactly one parent. - Each child entry is either
-1or a valid node identifier. - The tree can be extremely lopsided, for example a single chain of 200000 nodes, so your approach must cope with very deep trees.
- Aim for
O(n)time.
Examples
Example 1
Input: tree_height([[1, 2], [-1, -1], [3, -1], [-1, -1]])
Output: 3
Example 2
Input: tree_height([])
Output: 0
Hints
Hint 1
Height counts nodes, not edges: the root alone has height 1 and an empty tree has height 0.
Hint 2
Each node's level is its parent's level plus one, so you can carry the level along while you traverse and keep the largest value seen.
Hint 3
A chain of 200000 nodes will exceed Python's default recursion limit, so use an explicit stack (or a queue) instead of recursive calls.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
A node's level is determined by the path leading to it: the root is at level 1, and each child sits one level below its parent. So if we visit every node while remembering its level, the largest level we encounter is the answer. The deepest node is always a leaf (otherwise one of its children would be deeper), so no explicit leaf check is needed.
The classic recursive formulation, 1 + max(height(left), height(right)), would recurse up to 200000 levels on a chain-shaped tree. That blows past Python's default recursion limit, and raising the limit risks crashing the interpreter. Instead we use an iterative depth-first traversal with our own stack of (node, level) pairs, which lives on the heap.
Steps:
- If the table is empty, return
0. - Push
(0, 1)for the root. - Pop a pair, update the best level, and push each existing child (not
-1) withlevel + 1. - When the stack is empty, return the best level.
Why it is correct
- Because the input is a valid tree (all nodes reachable from the root, no cycles, one parent per non-root node), every node is pushed exactly once, so no visited set is required.
- Levels are correct by induction: the root is pushed with level 1, and a child's level is its parent's level plus one.
- The maximum level over all nodes equals the length, in nodes, of the longest root-to-leaf path, which is the required quantity. The traversal order (DFS or BFS) does not matter.
Complexity
- Time:
O(n), since each node is pushed and popped once with constant work. - Space:
O(n)in the worst case for the stack (a chain keeps it tiny; a bushy tree holds pending siblings).
Reference solution
from typing import List
def tree_height(child_table: List[List[int]]) -> int:
if not child_table:
return 0
best = 0
stack = [(0, 1)]
while stack:
node, level = stack.pop()
if level > best:
best = level
left, right = child_table[node]
if left != -1:
stack.append((left, level + 1))
if right != -1:
stack.append((right, level + 1))
return best