Bird
Raised Fist0

Identify the bug: ```python def maxDepth(root): if root is None: return 0 left = maxDepth(root.left) right = maxDepth(root.right) return left + right ```

medium🐞 Bug Identification Q7 of Q15
Tree: Depth-First Search - Maximum Depth of Binary Tree
The following recursive code attempts to compute the maximum depth of a binary tree. Identify the bug: ```python def maxDepth(root): if root is None: return 0 left = maxDepth(root.left) right = maxDepth(root.right) return left + right ```
ADoes not handle None root case
BReturns sum of left and right depths instead of max, overestimating depth
CForgets to add 1 for current node depth
DUses BFS instead of DFS
Step-by-Step Solution
Solution:
  1. Step 1: Analyze return statement

    Returns left + right instead of 1 + max(left, right).
  2. Step 2: Understand impact

    Sum overestimates depth by combining both subtree depths instead of choosing the deeper one.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Max depth requires max, not sum [OK]
Quick Trick: Max depth uses max(), not sum() [OK]
Common Mistakes:
MISTAKES
  • Summing depths instead of max
  • Ignoring base case
  • Off-by-one errors in depth calculation
Trap Explanation:
PITFALL
  • Candidates confuse max depth with total nodes count, summing depths incorrectly.
Interviewer Note:
CONTEXT
  • Tests ability to spot subtle logic bugs in recursive depth calculation.
Master "Maximum Depth of Binary Tree" in Tree: Depth-First Search

3 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Tree: Depth-First Search Quizzes