Dsa
Trees
Easy
Tree Height Calculator
Given a binary tree, write a program to calculate its height. The height of a tree is the number of edges in the longest path from the root node to a leaf node. If a tree has no nodes, its height is -1. If it only has a root node, its height is 0.
Input format:
A series of integers in level order, where 'N' represents a null node. For example, the input `1 2 3 N N 4 5` represents the tree:
1
/ \
2 3
/ \
4 5
Output format:
An integer representing the height of the tree.
Example:
Input:
1 2 3 N N 4 5
Output:
2
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