#C6357. Diameter of Binary Tree

    ID: 50108 Type: Default 1000ms 256MiB

Diameter of Binary Tree

Diameter of Binary Tree

You are given a binary tree represented in level order. The goal is to compute the diameter of the binary tree, which is defined as the length (number of edges) of the longest path between any two nodes.

The diameter can be expressed mathematically as:

\[ \text{diameter} = \max_{\text{for all nodes } u}\{(\text{height}(u.left) + \text{height}(u.right))\} \]

The input is provided as a single line of space-separated tokens representing the tree in level order. The token null represents an absent (empty) node.

Your task is to build the tree from the input, compute its diameter, and output the result.

inputFormat

The input is given in a single line containing space-separated tokens that represent the level order traversal of a binary tree. Each token is either an integer (node value) or the string null which indicates that the node is absent.

Examples:

  • 1 2 3 4 5
  • 1 2 null 3

outputFormat

Output a single integer: the diameter (the number of edges on the longest path) of the binary tree.

## sample
1 2 3 4 5
3

</p>