Take the root of a binary tree and return its maximum depth: the number of nodes on the longest path from the root down to a leaf, a node with no children. An empty tree has depth 0.
Example: the root 6 has a leaf, 3, on the left and 9 on the right; 9 has the children 7 and 12, and 12 has one child, 14, on its right. The longest path is 6, 9, 12, 14, so the answer is 4.
Try it yourself first: LeetCode 104 ↗. Then step through the solution below.
List every path from the root down to a leaf, then take the longest. Each path repeats the nodes above its leaf, so building them all costs up to O(n · h) time and space, for a tree of n nodes and height h. The recursion below never builds a path: each call returns one number.
The clue is in the words "longest path from the root down". Every such path starts at the root and goes on into one of its two subtrees. So the depth of a tree is 1, for the root, plus the depth of its deeper subtree.
Each subtree is the same problem on a smaller tree, which is what recursion is for: call the function on both children, add 1 to the larger answer, and stop at an empty tree, whose depth is 0.
Note
The VisiGrab app, like many textbooks, counts edges, not nodes: the root is at depth 0, and the height of this tree, the edges on its longest path down, is 3. LeetCode counts nodes, so its maximum depth is that height plus one: 4.
The player runs the code on the example. Press → for each step: the lines it runs light up in the code.
On a phone, Full screen keeps the picture and the code on one screen.
class TreeNode: def __init__(self, val, left=None, right=None): self.val, self.left, self.right = val, left, right def max_depth(root): if root is None: return 0 left_depth = max_depth(root.left) right_depth = max_depth(root.right) return 1 + max(left_depth, right_depth)
Now write it yourself on LeetCode 104 ↗, or go on: Same Tree →
left_depth, its left subtree's depth.left_depth.left_depth is 0.right_depth, its right subtree's depth.right_depth.right_depth.| Approach | Time | Space |
|---|---|---|
| Brute force: list every path from the root to a leaf | O(n · h) | O(n · h) |
| Count the levels with a queue | O(n) | O(w) |
| Recursion: 1 + the deeper subtree this page | O(n) | O(h) |
h is the tree's height in edges, one less than the depth this page returns, and w the number of nodes on its widest level.
Note
The deepest tree for its size is one long chain, and that is where this recursion fails: one call waits per node, and Python allows 1,000 nested calls by default, so a chain of about a thousand nodes raises RecursionError. Java and C++ stop only when the thread's stack runs out. The level count in the follow-up keeps its nodes in a queue instead, with no such limit.
The blocks below are steps of Maximum Depth of Binary Tree in words, and two of them do not belong. A block that works on a subtree “the same way” runs your lines again, from line 1, on that subtree. Put the right ones in order and check: your algorithm runs on the example and on an empty tree, so a mistake shows in its answer. Once it is right, the code appears beside your blocks.
Click the blocks in the order they run, or drag them into the lines.Tap the blocks in the order they run. Two of them don't belong.
Your algorithm
To find the depth of the tree under root:
Blocks
Now write it yourself on LeetCode 104 ↗, or go on: Same Tree →
class TreeNode: def __init__(self, val, left=None, right=None): self.val, self.left, self.right = val, left, right def max_depth(root): if root is None: return 0 left_depth = max_depth(root.left) right_depth = max_depth(root.right) return 1 + max(left_depth, right_depth)Can you find the depth without recursion? Yes: count the levels. Put the root into a queue and take out one whole level at a time, its children going in, as in Binary Tree Level Order Traversal (LeetCode 102). The number of levels is the depth: O(n) time, a queue of O(w) nodes, and no recursion limit.
What about the minimum depth? Minimum Depth of Binary Tree (LeetCode 111) is not this code with min in place of max. An empty child returns 0, so the example's 12 would report 1 + 0 = 1, as if it were a leaf. There, take the smaller depth of the children that exist, and 0 only for an empty tree.
What is the longest path between any two nodes? That is Diameter of Binary Tree (LeetCode 543). Make the same calls: at each node, left_depth + right_depth is the longest path through it, counted in edges. Keep the largest in a variable outside the calls, and still return 1 + the larger depth. On the example it is 4 edges, from 3 through 6, 9 and 12 to 14.
LeetCode is a trademark of LeetCode LLC. VisiGrab is not affiliated with LeetCode or with Blind; the problems are told in our own words, with our own examples.