Question bank › Trees
Dsa Trees Medium

Tree Height and Diameter

Given a binary tree, return its height and diameter. The height of a tree is the number of edges on the longest downward path between the root and a leaf. The diameter of a tree is the number of edges on the longest path between any two nodes in the tree. Input format: - The first line contains an integer N (1 ≤ N ≤ 1000), the number of nodes. - The next N lines contain two integers each: node value (unique) and its left and right children (use -1 when a child does not exist). Output format: - Print two integers separated by a space: height and diameter of the tree. Example input: 5 1 2 3 2 4 -1 3 -1 5 4 -1 -1 5 -1 -1 Example output: 2 3

Key concepts

binary treeheightdiameter

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.