#P4913. Calculate the Depth of a Binary Tree
Calculate the Depth of a Binary Tree
Calculate the Depth of a Binary Tree
Given a binary tree with \( n \) nodes (\( n \le 10^6 \)). Each node is described by two integers that denote the indices of its two child nodes. The tree is constructed starting with node \(1\) as the root. If a node is a leaf (has no children), the input for that node will be 0 0
.
The depth of the binary tree is defined as the maximum number of levels from the root node to any leaf node.
Your task is to compute and output the depth of the binary tree.
inputFormat
The first line contains an integer \( n \), the number of nodes in the tree. The following \( n \) lines each contain two integers representing the left child and the right child of the \( i^{th} \) node (\( 1 \le i \le n \)). A value of 0 indicates that the child does not exist.
outputFormat
Output a single integer representing the depth of the binary tree.
sample
1
0 0
1