Skip to main content
CodeOath
← All problems

Problem

Maximum Depth of Binary Tree

Easy
  • trees

Measure how deep the binary tree root goes and return that number. Count nodes, not edges: the depth is the number of nodes on the longest path that starts at the root and ends at a leaf. A tree with one node has depth 1, and an empty tree has depth 0.

Trees in this problem are written as level-order arrays: the root first, then each level from left to right. null marks a missing child, and a missing child has no entries beneath it. Trailing nulls are omitted.

Example 1
Input
root = [4, 2, 6, 1, null, null, 7, 0]
Output
4
Explanation

the longest path is 4, 2, 1, 0, which has four nodes. The path 4, 6, 7 on the right has only three.

Example 2
Input
root = []
Output
0
Explanation

an empty tree has no nodes.

Tab indents. Press Esc, then Tab to leave the editor.

Run your code to see every test here. Nothing is submitted or recorded.