Dsa
Trees
Warm-up
Tree Height Calculation
Given the root of a binary tree, write a function that returns the height of the tree. The height of a tree is the number of edges in the longest path from the root to a leaf. A tree with only one node has a height of 0.
Input format:
The first line contains an integer N, the number of nodes in the tree.
The next N lines each contain three integers: the node value, the left child value, and the right child value, where -1 indicates that the child does not exist.
Output format:
Output a single integer representing the height of the tree.
Example:
Input:
3
1 2 3
2 -1 -1
3 -1 -1
Output:
1
Key concepts
treesheightrecursion
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