Maximum Depth of Binary Tree
Count the levels from the top node to the deepest one
DSA, Easy, Trees
Which structure or algorithm, and what does it cost.
You get a binary tree tree. Your code may take the root node or the list. Each node has val, left and right, null when missing. Either is fine; no conversion is needed. The examples use a level-order list. It holds values row by row, left to right. Null marks a missing child. A missing node has no entries below it. An empty list is an empty tree. The depth of a tree is the number of nodes on the longest path from the root down to a leaf. Return the depth. An empty tree has depth 0.
Examples
Constraints
- 0 <= number of nodes <= 30,000.
- The tree can be one long chain.
Your answer must contain
- The simple method and its cost; the better method; the best method and why it is correct; code; time and memory cost; a hand trace of example 1.
Target Aim for O(n) time.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
Picture the tree drawn in rows. What does the depth say about the rows?
Hint 2 the approach
Depth is the number of rows. How can you move from one whole row to the next?
Hint 3 the key step
Keep the current row as a list. Count one for each row, then replace the list with all the children of its nodes. Stop when the list is empty.
Walkthrough
Full walkthrough brute force to best
Brute force
List every root-to-leaf path and keep the longest. A path can be as long as the tree is deep, and a tree can have many leaves. A chain of n / 2 nodes that ends in a full tree of n / 2 nodes has about n / 4 leaves, and each path is at least n / 2 nodes long. That is O(n^2) time.
Better method
Recursion: the depth of a node is 1 plus the larger depth of its two children, and the depth of a missing node is 0. This visits each node once, so it is O(n) time with O(h) call stack, where h is the height. On a chain of 30,000 nodes the call stack is as deep as the chain and can overflow.
Best method
The depth equals the number of rows when the tree is drawn level by level. Start with the row that holds the root. Count one, then build the next row from the children of the nodes in the current row. Repeat until a row is empty. Each node is placed in a row once, so the work is linear. There is no recursion, so a deep chain is safe. The same row-by-row loop answers any question about levels.
Hand trace
Run on [8, 3, 10, null, 6, null, 14, 5]. Row 1 is [8], depth 1. Its children give row 2: [3, 10], depth 2. Node 3 has only a right child 6, and node 10 has only a right child 14, so row 3 is [6, 14], depth 3. Node 6 has a left child 5 and node 14 has none, so row 4 is [5], depth 4. Node 5 has no children, so row 5 is empty and the loop stops. The depth is 4.
Code
def max_depth(root):
level, depth = [root] if root else [], 0
while level:
depth += 1
level = [child for node in level for child in (node.left, node.right) if child]
return depthTime and memory
O(n) time, because every node is placed into one row and read once. O(w) extra memory for the current row, where w is the widest row. That is at most n, and 1 for a chain.
Common mistakes
- Counting edges instead of nodes. A single node has depth 1, not 0.
- Forgetting the empty tree. With [] there is no row at all, so the answer is 0.
- Taking the depth of only the left side. A deeper path can sit on the right, so look at every row.