Easy
017Maximum Depth of Binary Tree
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
3FUNCTION SHAPE
root: intArray→intSOLUTION 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))Time
O(n)Space
O(h)