← DiffPush

Height of Binary Tree / Maximum Depth

Standard Bar Binary Trees · Medium Problems O(n) · O(h)

The Tallest Relay Mast Count

A broadcast network diagrams its relay masts as a binary tree, with the studio at the root and each mast feeding at most two downstream masts. Signal-latency budgeting depends on one number: how many masts a packet traverses on the longest root-to-leaf chain. The network planner needs that count for every redesigned topology before signing off on hardware.

Input: The root of a binary tree given as a level-order array where null marks a missing child.

Output: Return an integer: the number of nodes on the longest root-to-leaf path.

Constraints

Examples

Example 1

Input: {"tree":[3,9,20,null,null,15,7]}
Output: 3
The deepest chain is 3 -> 20 -> 15 (or -> 7): three nodes.

Example 2

Input: {"tree":[1,2]}
Output: 2
Root plus its single left child spans two nodes.

Solve this in your browser →

Also on LeetCode ↗