Bird
Raised Fist0

Consider the BFS-based maxDepth function. What is the output when the input tree is empty (root = None)?

medium🧾 Code Trace Q4 of Q15
Tree: Depth-First Search - Maximum Depth of Binary Tree
Consider the BFS-based maxDepth function. What is the output when the input tree is empty (root = None)?
A1
B-1
C0
DThrows an exception
Step-by-Step Solution
Solution:
  1. Step 1: Check base case

    Function returns 0 immediately if root is None.
  2. Step 2: Confirm no further processing

    No queue operations occur; depth remains 0.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Empty tree depth is zero by definition [OK]
Quick Trick: Empty tree depth = 0 [OK]
Common Mistakes:
MISTAKES
  • Returning 1 for empty tree
  • Not handling None root
  • Code crashes on empty input
Trap Explanation:
PITFALL
  • Some candidates forget to handle null root, causing exceptions or wrong depth.
Interviewer Note:
CONTEXT
  • Checks handling of edge cases and base conditions in BFS code.
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