Facebook PixelMaximum Depth of Binary Tree — Coding Practice
Maximum Depth of Binary TreeEasy

Maximum Depth of Binary Tree

Easy 11.8k75% acceptance
TreeDepth-First SearchBinary Tree

Given the root of a binary tree, return its maximum depth — the number of nodes along the longest path from the root down to the farthest leaf.

The tree is given in level-order (TreeNode with { val, left, right }, null for a missing child).

Example 1
Input: root = [3,9,20,null,null,15,7]
Output: 3
Example 2
Input: root = [1,null,2]
Output: 2
Example 3
Input: root = []
Output: 0
An empty tree has depth 0.
Constraints
  • The number of nodes is in the range [0, 10⁴].
  • -100 ≤ Node.val ≤ 100
Asked atAmazonGoogleLinkedIn
JavaScript
Loading editor…
Case 1
[3,9,20,null,null,15,7]
expected: 3
Case 2
[1,null,2]
expected: 2
Case 3
[]
expected: 0
Case 4
[1]
expected: 1
Case 5
[1,2,3,4,null,null,5]
expected: 3