Compute the height
Warm-up · Trace the algorithm · about 5 min
Each node's height is 1 more than the larger height of its children; a leaf has height 0. Follow the trace on the tree below and fill in each height before the step plays.
Example 1
Input:
{"levelOrder":[1,2,3,4,5,null,6,null,null,7]}
Output:
3
The longest path is 1, 2, 5, 7.
Constraints
- Height counts edges
- The trace uses the tree shown
Learn the idea first: Trees: root, leaf, depth, height, subtree, a visual lesson.
More Trees: root, leaf, depth, height, subtree problems
- Label the parts of a tree Warm-up
- Count the leaves Standard
- Height of a binary tree Standard
- Count the leaves in code Standard
- Count the nodes at depth d Standard
- Nodes at depth d in a full tree Challenge