← Whiteboard questions

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

Input tree = [8, 3, 10, null, 6, null, 14, 5]
Output 4
Why the longest path is 8, 3, 6, 5. That is 4 nodes.
Input tree = [2, null, 9, null, 1]
Output 3
Why the tree is a chain of 3 nodes, so every level holds one node.
Input tree = []
Output 0
Why an empty tree has no nodes.

Constraints

Your answer must contain

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 depth

Time 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.

Related problems

More Trees problems
Maximum Depth of Binary Tree: walkthrough and solution