#P4913. 【深基16.例3】二叉树深度

【深基16.例3】二叉树深度

Problem Description

There is a binary tree with n(n106)n(n \le 10^6) nodes. You are given the indices of the two child nodes for each node (both not exceeding nn). Build a binary tree (the root node has index 11). If a node is a leaf, the input is 0 0.

After building the binary tree, find its depth. The depth of a binary tree means the maximum number of levels passed when going from the root node to a leaf node.

Input Format

The first line contains an integer nn, which is the number of nodes.

Then follow nn lines. In the ii-th line, two integers ll and rr are given, representing the indices of the left and right child nodes of node ii, respectively. If l=0l=0, it means there is no left child node; similarly, r=0r=0 means there is no right child node.

Output Format

One integer, representing the maximum node depth.

7
2 7
3 6
4 5
0 0
0 0
0 0
0 0
4

Hint

Translated by ChatGPT 5