Maximum Depth of Binary Tree

Given the root of a binary tree, return its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Pattern focus: Recursive Tree Metrics. Compute the depth of each subtree and take the maximum.

Input Format

root = binary tree root

Output Format

maximum depth of the tree

Constraints

  • 1 <= number of nodes <= 10^5
  • -10^4 <= node values <= 10^4
  • Input must satisfy the format described in inputFormat.

Examples

Example 1:

Input:

root = [3,9,20,null,null,15,7]

Output:

3

Explanation:

The longest root-to-leaf path has 3 nodes.

Example 2:

Input:

root = [1,null,2,3]

Output:

3

Explanation:

The path 1 -> 2 -> 3 has depth 3.

Loading...
Maximum Depth of Binary Tree - Binary Tree