A-047● Day 472026.06.22easyleetcode #104neetcode150
Maximum Depth of Binary Tree
#tree#depth-first-search#breadth-first-search#binary-tree
01
Problem
· problemGiven 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
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 tree│ nested
○
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
· solvemental 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.