#K33222. Binary Tree Height
Binary Tree Height
Binary Tree Height
Given a binary tree, your task is to compute its height. The height is defined as the number of edges on the longest path from the root to a leaf node. In mathematical terms, if we denote the height of an empty tree as \(-1\), then for any non-empty tree with root \(r\) and subtrees \(L\) and \(R\), the height is:
[ \text{height}(r) = 1 + \max(\text{height}(L),\ \text{height}(R)) ]
The input will describe the binary tree in a structured format (see Input Description). Notice that if the tree is empty, the height should be \(-1\).
inputFormat
The first line contains an integer \(n\) representing the number of nodes in the tree. If \(n = 0\), the tree is empty.
Otherwise, the next \(n\) lines each contain three space-separated integers: \(v\), \(l\), and \(r\). Here \(v\) is the value of the node, \(l\) is the 1-indexed index of its left child, and \(r\) is the 1-indexed index of its right child. If a child does not exist, its index is given as \(-1\). The node with index 1 is the root of the tree.
outputFormat
Output a single integer representing the height of the binary tree.
## sample0
-1
</p>