Binary tree diameter · Compiler

Binary tree diameter

Compute the longest simple path between any two nodes of a binary tree. Measure its length in edges. The path may lie entirely within one subtree.

Input
The first line contains n (1 <= n <= 100). Nodes are numbered 1 through n with root 1. For each i from 1 through n, the next line gives the left and right child IDs of node i, using 0 for a missing child. The input forms one valid tree.
Output
Print the diameter in edges.

Compiler Explorer runs your code when you run or check it.

Loading your exercise draft…