ExamAdda Logo

Maximum Depth of Binary Tree

Easy

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

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

For an empty tree, the depth is 0.

Example 1

Input

root = [3,9,20,null,null,15,7]

Output

3

Explanation

The longest path is:

3 → 20 → 15

 

or

 

3 → 20 → 7

So the maximum depth is 3.

Example 2

Input

root = [1,null,2]

Output

2

Explanation

The longest path is:

1 → 2

 

Therefore, the maximum depth is 2.

Constraints

0 <= number of nodes <= 10^4
-100 <= Node.val <= 100

Hints:

Hint 1

The depth of a node is:

1 + maximum depth of its left and right subtrees

Hint 2

Use recursion:

maxDepth(root) = 1 + max(maxDepth(root.left), maxDepth(root.right))

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 11, 2026

Last updated

Aug 12, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output