Bird
Raised Fist0

Consider the optimized recursive diameter code. What is the diameter returned for a single-node tree (root only)?

medium🧾 Code Trace Q4 of Q15
Tree: Depth-First Search - Diameter of Binary Tree
Consider the optimized recursive diameter code. What is the diameter returned for a single-node tree (root only)?
A1
B2
C0
DNone
Step-by-Step Solution
Solution:
  1. Step 1: Trace height for single node

    Height of root is 1, left and right children are null with height 0.
  2. Step 2: Calculate diameter

    Diameter = max(0, 0 + 0) = 0, so function returns 0.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Diameter of single node tree is 0 edges [OK]
Quick Trick: Diameter counts edges, single node -> 0
Common Mistakes:
MISTAKES
  • Returning 1 instead of 0 for single node
  • Confusing nodes with edges in diameter
Trap Explanation:
PITFALL
  • Candidates often return 1 for single node diameter, forgetting diameter counts edges, not nodes.
Interviewer Note:
CONTEXT
  • Tests handling of edge cases in recursive diameter code
Master "Diameter 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