Dsa
Trees
Warm-up
Height of a Tree
Given the root of a binary tree, return its height. The height of a tree is defined as the number of edges on the longest path from the root to a leaf. If the tree is empty, return -1.
Input format:
A single line containing integers, where the first integer is the number of nodes in the tree followed by the values of the nodes in level order (use -1 to represent null nodes).
Output format:
Return the height of the tree as an integer.
Example:
Input:
7 1 2 3 4 5 -1 -1 -1 -1 -1
Output:
2
Key concepts
treesheightbinary trees
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