Tree Height and Peak Concurrent Bookings
Trees & GraphsAuto-graded · 22 test casesLast updated
Compute the height of an index-encoded binary tree without deep recursion, and the minimum rooms needed for half-open time intervals, returned together as a pair.
Write a single function that answers two independent questions and returns both answers in a list.
Part A: Height of a binary tree
A binary tree is given as an index-based table child_table:
child_table[i] = [left, right]holds the indices of the left and right children of nodei.- A value of
-1means that child does not exist. - Node
0is the root. - An empty
child_tablemeans the tree is empty.
The height of the tree is the number of nodes on the longest path that starts at the root and ends at a leaf. An empty tree has height 0, and a tree with only a root has height 1.
The tree can contain up to 100,000 nodes and may be extremely lopsided (for example, a single long chain), so your solution should not rely on deep recursion.
Part B: Fewest rooms for a set of bookings
You are given bookings, a list of [start, end) integer pairs. Each booking occupies a room from start (inclusive) up to end (exclusive). Because the ranges are half-open, a booking that ends at time t does not conflict with another booking that starts at time t; they may share a room.
Find the smallest number of rooms such that every booking can be placed in a room with no two overlapping bookings in the same room. If there are no bookings, the answer is 0.
Output
Return a list [height, rooms], where height is the answer to Part A and rooms is the answer to Part B.
Constraints
0 <= len(child_table) <= 100000- Each entry of
child_tablehas exactly two integers, each either-1or a valid node index; the structure is a valid binary tree rooted at node0. 0 <= len(bookings) <= 100000- For every booking,
start < end, and both are integers. - The two inputs are unrelated to each other.
Examples
Example 1
Input: tree_height_and_min_rooms([[1, 2], [-1, -1], [3, 4], [-1, -1], [-1, -1]], [[0, 30], [5, 10], [15, 20]])
Output: [3, 2]
Example 2
Input: tree_height_and_min_rooms([], [[7, 10], [10, 12]])
Output: [0, 1]
Hints
Hint 1
For the height, each node's level is its parent's level plus one. Track the level alongside each node using an explicit stack (or a queue) rather than recursion.
Hint 2
The rooms needed equal the largest number of bookings active at the same moment. That peak can only increase at a start time.
Hint 3
Sort the start times and the end times separately. As you process each start, first free every booking whose end is less than or equal to that start, then add the new one and track the maximum.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The two parts are independent, so we solve each one and return [height, rooms].
Part A: height
Each node sits one level below its parent, with the root at level 1. The height is therefore the largest level of any node. Recursion would work in principle, but a chain of 100,000 nodes would exceed Python's default recursion limit. Instead, use an explicit stack of (node, level) pairs:
- If the table is empty, the height is
0. - Push
(0, 1). - Pop a pair, update the running maximum with its level, and push each existing child with
level + 1.
Every node is visited exactly once because a tree has a unique path to each node. The deepest node is necessarily a leaf, so the maximum level equals the node count of the longest root-to-leaf path.
Part B: rooms
The minimum number of rooms equals the maximum number of bookings in progress at any single moment. That maximum can only be reached at some booking's start time, since the count only goes up when a booking starts.
- Sort all start times, and separately sort all end times.
- Go through the starts in order. Before counting a new booking, release every booking whose end is
<= startusing a forward-moving pointer into the sorted ends. - Add the new booking to the active count and update the maximum.
The <= is essential: with half-open intervals, a booking that ends at t frees its room for one starting at t. Separating starts and ends is safe because only the number of active bookings matters, not which specific booking ended. At each start s, after releasing, the active count plus one equals the number of bookings covering time s.
Complexity
- Part A: O(n) time, O(n) space for the stack.
- Part B: O(m log m) time for sorting, O(m) space, where m is the number of bookings.
Reference solution
from typing import List
def tree_height_and_min_rooms(child_table: List[List[int]], bookings: List[List[int]]) -> List[int]:
# Part A: iterative DFS tracking the level (counted in nodes).
height = 0
if child_table:
stack = [(0, 1)]
while stack:
node, level = stack.pop()
if level > height:
height = level
left, right = child_table[node]
if left != -1:
stack.append((left, level + 1))
if right != -1:
stack.append((right, level + 1))
# Part B: sweep over separately sorted starts and ends.
if not bookings:
return [height, 0]
starts = sorted(s for s, e in bookings)
ends = sorted(e for s, e in bookings)
active = 0
rooms = 0
end_ptr = 0
for s in starts:
while end_ptr < len(ends) and ends[end_ptr] <= s:
active -= 1
end_ptr += 1
active += 1
if active > rooms:
rooms = active
return [height, rooms]