DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
Back to Arena
easy
Tree

Diameter of Binary Tree

**Problem Statement:** Given the required input arguments, write an efficient algorithm to solve the **Diameter of Binary Tree** problem. Implement the required logic as specified by standard definitions for this classic algorithmic challenge. **Hint / Expected Approach:** DFS returning height; track max left+right **Edge Cases to Consider:** - (1) Single node (diameter=0) - (2) Linear tree - (3) Diameter path doesn't pass through root

Examples

Example 1
Input: [1,2,3,4,5]
Output: 3
Explanation: Path 4-2-1-3 or 5-2-1-3
Example 2
Input: [1,2]
Output: 1
Explanation: Single edge
Example 3
Input: [1]
Output: 0
Explanation: Single node

Constraints

  • ▪The number of nodes in the tree is in the range [0, 2000]
  • ▪-1000 <= Node.val <= 1000
  • ▪Ensure depth-first or breadth-first traversals are memory-efficient

Watch Out For Edge Cases

  • ▪Single node (diameter=0)
  • ▪Linear tree
  • ▪Diameter path doesn't pass through root
Frequently Asked At
AmazonGoogleMicrosoftAdobeFlipkart