Skip to content
EasyTreesAI interview only

Maximum Depth of Binary Tree

Asked atamazongooglelinkedinappleyahoo

01 · 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.

02 · Examples

Example 01
    3
   / \
  9  20
    /  \
   15   7
Input
root = [3,9,20,null,null,15,7]
Output
3

The tree has 3 levels. The longest path is 3 -> 20 -> 15 (or 3 -> 20 -> 7), which has 3 nodes.

Example 02
  1
   \
    2
Input
root = [1,null,2]
Output
2

The tree has 2 levels. The root is 1, and its right child is 2. The longest path has 2 nodes.

03 · Constraints

  • 01The number of nodes in the tree is in the range [0, 104].
  • 02-100 <= Node.val <= 100

04 · Optimal complexity

Time
O(n)
Space
O(h)
05 · Two ways to work on it

Practice it alone or rehearse it as an interview.

Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.