AlgoLounge

Maximum Depth of Binary Tree

easy

Leetcode 104. Maximum Depth of Binary Tree

Given the root of a binary tree, return its depth.

The depth of a binary tree is defined as the number of nodes along the longest path from the root node down to the farthest leaf node.

Examples

Example 1

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

3 / \ 9 20 / \ 15 7Output: 3 (depth: 3 levels)

Example 2

Input:  root = [1,null,2]

1 \ 2Output: 2

Example 3

Input:  root = []
Output: 0

Constraints

  • 0 <= The number of nodes in the tree <= 100
  • -100 <= Node.val <= 100

How the tests work: trees are written as level-order arrays — [1,null,2] is a root 1 with a right child 2. Your function receives real TreeNode objects.

TypeScript
Loading editor…
List of test cases (5)
Case 1
root = [3,9,20,null,null,15,7]
Case 2
root = [1,null,2]
Case 3
root = []
Case 4
root = [1]
Case 5
root = [1,2,3,4,5]