Compute binary-tree diameter via return-only DFS
Company: Meta
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates algorithmic problem-solving with binary trees, focusing on tree traversal techniques, recursion versus iterative implementations, and analysis of time and space complexity including worst-case input shapes such as skewed trees.
Constraints
- Inputs are provided as Python literals matching the function signature.
- Return a deterministic exact-match result.
Examples
Input: ([1,2,3,4,5],)
Expected Output: 3
Explanation: Diameter through root.
Input: ([],)
Expected Output: 0
Explanation: Empty tree.
Input: ([1],)
Expected Output: 0
Explanation: Single node.
Input: ([1,2,None,3,None,4],)
Expected Output: 2
Explanation: Skewed tree.
Hints
- Choose a representation that makes the core operation simple.
- Handle empty and boundary inputs before the main algorithm.