Skip to content
CodeKruPro

Maximum depth (height) of a binary tree

Coding problemEasyAsked in: Adobe, Amazon, Apple, Google, Meta, Microsoft

Given the root of a binary tree, return its maximum depth: the number of nodes on the longest path from the root down to a leaf. A tree with only a root has depth 1, and an empty tree has depth 0.

Trees are written in level order, with null for a missing child.

Examples

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

Constraints

  • The tree has between 0 and 100,000 nodes
  • Each value is a 32-bit integer

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

to answer and keep your progress. Reading is free for everyone.