Question bank › Trees
Dsa Trees Medium

Diameter of Binary Tree

Calculate the diameter of a binary tree. The diameter is defined as the number of nodes on the longest path between two leaves in the tree. Input format: - The first line contains an integer n, the number of nodes in the binary tree. - The next n lines each contain three integers indicating the parent node's value, the left child's value (or -1 if no left child), and the right child's value (or -1 if no right child). Output format: - Output the diameter of the binary tree. Example: Input: 4 1 2 3 2 4 -1 3 -1 5 4 -1 -1 5 -1 -1 Output: 4

Key concepts

treesdepth-first searchrecursion

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
Part of Praxari's verified interview question bank. We show the prompt and concepts to practise with — never a copy-paste solution.