Get DOM Tree Height

Easy
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Get DOM Tree Height

Approach

Use DFS recursion where each leaf node returns height 1, and each internal node returns 1 plus the maximum height among its children. Also implement a BFS alternative that tracks depth level-by-level using a queue.

Complexity Analysis

Time
O(n) where n is the number of nodes
Space
O(h) where h is the tree height

Pattern

Tree Height Calculation

Why It Works

Recursively computing 1 + max(children heights) propagates the longest root-to-leaf path upward, while the BFS variant naturally discovers the maximum depth as it processes each level.

Updated Feb 2026