AAlgoLoopSpaced repetition for LeetCode
EASYTree / RecursionLeetCode ↗

Maximum Depth of Binary Tree

The key idea

The depth of a tree is 1 plus the larger of its two subtree depths. An empty subtree has depth 0. This self-referential definition turns directly into a recursion: solve each child, take the max, add one for the current node.

Problem

Given the root of a binary tree, return its maximum depth.

A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Constraints

Examples

Input: root = [3,9,20,null,null,15,7] Output: 3
Input: root = [1,null,2] Output: 2

Complexity

Time: O(n) Space: O(h)

See the full solution

410310
Step-by-step visualization
Start free →

More Tree / Recursion problems