Back

Maximum Depth of Binary Tree

easy

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.

Test Cases

Copy an input into the main harness and Run to verify
Input
root = [3,9,20,null,null,15,7]
Expected Output
3
Input
root = [1,null,2]
Expected Output
2

Constraints

  • The number of nodes in the tree is in the range [0, 10^4].
  • -100 <= Node.val <= 100

Hints

Hint 1 — click to reveal

Depth of a tree = 1 + max(depth(left), depth(right)).

Hint 2 — click to reveal

A BFS level count also works.

Java Compiler

Powered by OneCompiler. Starter code loads automatically — edit and hit Run.