#K891. Diameter of a Binary Tree

    ID: 37455 Type: Default 1000ms 256MiB

Diameter of a Binary Tree

Diameter of a Binary Tree

You are given a binary tree in level-order traversal. Each node is represented by an integer and null nodes are represented by the string null. Your task is to compute the diameter of the binary tree. The diameter is defined as the number of nodes on the longest path between two nodes in the tree.

You can use the following formula to understand the computation:

\(diameter = \max_{node}(depth(left_{subtree}) + depth(right_{subtree})) + 1\)

The input is provided in a single line representing the level order traversal of the tree, and the output should be a single integer denoting the diameter.

inputFormat

The input consists of a single line containing space-separated tokens. Each token is either an integer (representing a node's value) or the string null (representing a missing node). This representation is a level-order traversal of the binary tree.

Examples:

  • null represents an empty tree.
  • 1 represents a tree with only one node.
  • 1 2 3 4 5 null null represents the binary tree:
       1
      / \
     2   3
    / \
   4   5

outputFormat

Output a single integer which is the diameter of the binary tree. For an empty tree, output 0.

## sample
null
0

</p>