Question bank › Trees
Dsa Trees Easy

Diameter of a Tree

Calculate the diameter of a binary tree. The diameter is defined as the length of the longest path between any two nodes in the tree, which 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 following N lines contain integers representing each node's value in level-order traversal. Use -1 to indicate 'null' nodes. Output format: - Print a single integer representing the diameter of the tree. Example: Input: 5 1 2 3 -1 -1 4 -1 -1 Output: 3

Key concepts

treesdiameterdepth

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