Dsa
Trees
Medium
Tree Diameter Calculation
The diameter of a binary tree is the length of the longest path between any two nodes in the tree. Write a function that reads a binary tree from standard input in level-order format (using '#' for null nodes) and prints the diameter length.
Input format:
- The first line contains a space-separated list of values representing the binary tree in level-order.
Output format:
- A single integer representing the length of the tree diameter.
Example:
Input:
1 2 3 4 # # 5 # # #
Output:
4
Key concepts
treesdepth-first searchdiameter
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now