Back to the 100
Problem 017Trees
Easy

Maximum Depth of Binary Tree

017

Given a binary tree encoded as level-order with -100000 as null, return its maximum depth.

EXAMPLES

Example 1
Input
{
  "root": [
    3,
    9,
    20,
    -100000,
    -100000,
    15,
    7
  ]
}

Output
3

FUNCTION SHAPE

root: intArrayint
SOLUTION NOTE

Postorder. The max depth at a node is the max of left and right depths + 1 for the current node.

Reveal reference solution +
pythonREFERENCE
def maxDepth(self, root: TreeNode) -> int:
    if not root:
        return 0
    return 1 + max(self.maxDepth(root.left),
                   self.maxDepth(root.right))
TimeO(n)
SpaceO(h)
Open on LeetCode
00:00
3 local tests readyRun with ⌘/Ctrl + Enter. Your code stays in this browser.

Runs solve(...) locally in a browser worker. SWE Playbook does not submit your code. Only run code you trust; Python code may access the network.