The problem. Given the root of a binary tree, return its maximum depth: the number of nodes on the longest path from the root down to a leaf. An empty tree has depth 0.
Input: root = [3, 9, 20, null, null, 15, 7] Output: 3
Input: root = [1, null, 2] Output: 2
Input: root = [] Output: 0LeetCode's "depth" counts nodes, so it equals the height in edges plus one. The question is really: how many levels does the tree have?
Members only · free account
The rest of this lesson, with 6 more sections, 1 drawing, a dry-run simulator and code in JavaScript, Python, Java and C++, opens with a free account.
Not ready yet? The Arrays course is free to read, no account needed.
In the rest of this lesson