all entries
A-047Day 472026.06.22easyleetcode #104neetcode150

Maximum Depth of Binary Tree

#tree#depth-first-search#breadth-first-search#binary-tree
leetcode #104 · maximum-depth-of-binary-tree
01

Problem

· problem
P.047

Maximum Depth of Binary Tree

leetcode #104

Given the root of a binary tree, return its maximum depth. A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

constraints
  • · 0 ≤ number of nodes ≤ 10^4
  • · -100 ≤ Node.val ≤ 100
// paraphrased summary — see source for full text
examples
example 1input → output
[3,9,20,null,null,15,7]
3
example 2input → output
[1,null,2]
2
02

Pre-solve

· pre-solve
● 1list shown● 2select● 3reveal
  • Does an empty tree (root = null) have maximum depth of 0?
  • Is maximum depth counted in nodes or edges along the longest path?
  • What is the depth of a tree containing only the root node?
  • Should null nodes contribute to the depth count?
  • Must we visit every single node to find the maximum depth?
  • Can we solve this iteratively using an explicit stack?
check the items you would ask, then press confirm
// session-only state — refresh resets (repeatable practice)
03

Logic Structure

· logic
● 1slots shown● 2pick per slot● 3reveal
// pick one code line per slot to assemble the algorithm flow. no typing — just the logic skeleton.
step 1· Base case: Check if tree is empty
if not root:
if root.left is None and root.right is None:
if root is not None:
step 2· Base case: Return value for empty treenested
return 0
return 1
return -1
step 3· Recursive call: Explore left subtree
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
self.maxDepth(root.left.left)
self.maxDepth(root.right)
step 4· Combine: Take maximum of both subtrees
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
left_depth + right_depth
max(left_depth, right_depth)
step 5· Complete return: Add current node to best subtree
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
return 1 + self.maxDepth(root.left) + self.maxDepth(root.right)
return max(self.maxDepth(root.left), self.maxDepth(root.right))
pick one option per slot
// format: slot — recursive / DP patterns use ordering / state-first formats. ADR-08 follow-up.
04

Solve · Trace

· solve
solution.py
1
# RECURSIVE DFS
2
class Solution:
3
    def maxDepth(self, root: TreeNode) -> int:
4
        if not root:
5
            return 0
6
7
        return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
8
9
10
# ITERATIVE DFS
11
class Solution:
12
    def maxDepth(self, root: TreeNode) -> int:
13
        stack = [[root, 1]]
14
        res = 0
15
16
        while stack:
17
            node, depth = stack.pop()
18
19
            if node:
20
                res = max(res, depth)
21
                stack.append([node.left, depth + 1])
22
                stack.append([node.right, depth + 1])
23
        return res
24
25
26
# BFS
27
class Solution:
28
    def maxDepth(self, root: TreeNode) -> int:
29
        q = deque()
30
        if root:
31
            q.append(root)
32
33
        level = 0
34
35
        while q:
36
37
            for i in range(len(q)):
38
                node = q.popleft()
39
                if node.left:
40
                    q.append(node.left)
41
                if node.right:
42
                    q.append(node.right)
43
            level += 1
44
        return level
mental dry-run cases
// walk each case in your head; expand the worked example below if stuck.
case 1
[3,9,20,null,null,15,7]
3
case 2
[1,null,2]
2
// UI does not walk-through — you do the dry-run mentally. Expand the worked example if stuck.