Maximum Depth of Binary Tree

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: Maximum Depth of Binary Tree

Approach

Base case returns 0 for null. For non-null node, return 1 + max depth from either subtree.

Complexity Analysis

Time
O(n) where n is node count
Space
O(h) where h is tree height

Pattern

Tree Height Calculation

Why It Works

The deepest path must come from either left or right subtree plus the current node.

Updated Mar 2026