#K8881. Diameter of a Binary Tree

    ID: 37391 Type: Default 1000ms 256MiB

Diameter of a Binary Tree

Diameter of a Binary Tree

Given a binary tree, your task is to compute its diameter. The diameter of a tree is defined as the number of edges in the longest path between any two nodes. The path may or may not pass through the root.

The binary tree is represented by n nodes. Each node is described by a tuple of three integers: \( (x, l, r) \) where \( x \) is the value (or index) of the node, and \( l \) and \( r \) are the values of its left and right children respectively. A value of \(-1\) indicates that the corresponding child is absent.

Note: It is guaranteed that the node values are from 1 to \( n \) and are given such that the node with value 1 is always the root of the tree.

Your task is to compute and print the diameter of this binary tree.

inputFormat

The first line contains an integer \( n \) (number of nodes in the tree).

The following \( n \) lines each contain three space-separated integers \( x \), \( l \), and \( r \), representing the node value, the value of the left child, and the value of the right child respectively. A child value of \(-1\) indicates that the child is missing.

outputFormat

Output a single integer, the diameter of the binary tree.

## sample
5
1 2 3
2 4 -1
3 -1 5
4 -1 -1
5 -1 -1
4