Asked at

Maximum Depth Of Binary Tree

Easy
Verified
TreeBFSDFS~12 min

Given the root of a binary tree, return its maximum depth: the number of nodes along the longest path from root to a leaf.

A node is { val, left, right } with null children; the whole empty tree is null. The input arrives as the root node directly.

Examples

in{val:3, left:{val:9}, right:{val:20, left:{val:15}, right:{val:7}}}
out3

Longest root-to-leaf path 3 → 20 → 15 has 3 nodes.

innull
out0

An empty tree has depth 0.

Constraints

  • 0 ≤ number of nodes ≤ 10⁴
  • -100 ≤ node.val ≤ 100

Get help

🔑

Sign in to solve

Sign in to write, run, and submit your solution — and to pick up where your iOS flow left off.