Skip to content
Onsite

Maximum Depth of Binary Tree

EasyP1Coding

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. An empty tree has depth 0.

Tree nodes have val, left and right. The class TreeNode is already defined for you. The tree arrives in level-order form (as in the examples) and you receive its root, which is null for an empty tree.

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 number of nodes is in the range [0, 10^4].
  • -100 <= Node.val <= 100

Hints

Run checks the 3 visible tests, Submit all 9.