Dsa
Binary Search Tree
Hard
Binary Search Tree Diameter
The diameter of a binary tree is defined as the length of the longest path between any two nodes in the tree. This path may or may not pass through the root.
Input Format:
The first line contains an integer n, the number of nodes in the tree.
The second line contains n space-separated integers representing the values of the nodes in a pre-order traversal (-1 for null).
Output Format:
Print a single integer representing the diameter of the tree.
Example:
Input:
5
1 2 3 -1 -1 4 -1 -1 5 -1 -1
Output:
3
Key concepts
binary_treediameterdepth
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