#K95287. Longest Consecutive Path in a Binary Tree

    ID: 38830 Type: Default 1000ms 256MiB

Longest Consecutive Path in a Binary Tree

Longest Consecutive Path in a Binary Tree

You are given a binary tree in which each node contains an integer value. Your task is to find the length of the longest downward path (from parent to child) that consists of consecutive integers. In other words, for any two consecutive nodes in the path, the child's value must be exactly one more than its parent's value.

The consecutive relationship between a node with value \(v\) and its parent with value \(u\) can be expressed by the equation \(v = u + 1\).

The tree is provided as a level order traversal where a token null represents a missing node.

inputFormat

The input is a single line containing space-separated tokens representing the binary tree in level order. Use null to indicate that a node is missing.

For example, the input 1 null 3 2 4 null 5 corresponds to the binary tree below:

    1
     \
      3
     / \
    2   4
         \
          5

outputFormat

Output a single integer — the length of the longest consecutive path in the binary tree.

## sample
1 null 3 2 4 null 5
3