PIXELBANKv8.2.1
Menu

Maximum Depth of Binary Tree

EasyTrees

Given the root of a binary tree (as a level-order array), return its maximum depth (number of nodes along the longest root-to-leaf path).

Example:

Input:
3,9,20,null,null,15,7
Output:
3
Reasoning:
  • The input array represents a binary tree in level-order traversal: 3 is the root, 9 and 20 are its children, and 15 and 7 are the children of 20.
  • The tree structure is:
    • 3 (root)
    • / \
    • 9 20
    • / \
    • 15 7
  • We calculate the depth of each path: the path 3 -> 9 has a depth of 2, and the path 3 -> 20 -> 15 (or 7) has a depth of 3.
  • The maximum depth among all paths is 33, which corresponds to the longest root-to-leaf path (3 -> 20 -> 15 or 3 -> 20 -> 7).
  • The final output is 3, representing the maximum depth of the binary tree.

Constraints:

  • 0 <= number of nodes <= 10^4
  • -100 <= Node.val <= 100
Editor

Test Results

0/0
Run code to see test results.