#K77782. Cousin Nodes in a Binary Tree

    ID: 34941 Type: Default 1000ms 256MiB

Cousin Nodes in a Binary Tree

Cousin Nodes in a Binary Tree

Given a binary tree and two integer values x and y, determine whether the nodes corresponding to these values are cousins. Two nodes in a binary tree are considered cousins if they are at the same depth (level) but have different parents.

The binary tree is provided as a level-order traversal with the string null denoting missing nodes. The challenge is to build the binary tree from the given input, find the depths and parents of the nodes with values x and y, and then check if they are cousins.

Definition:

Let d(n) be the depth of node n and p(n) be the parent of node n. Nodes x and y are cousins if and only if:

$$ d(x) = d(y) \quad \text{and} \quad p(x) \neq p(y). $$

inputFormat

The input consists of two lines:

  • The first line contains a space-separated list of values representing the level-order traversal of the binary tree. Use the string null (case-sensitive) to represent a missing node.
  • The second line contains two integers x and y separated by a space.

outputFormat

Output True if the nodes with values x and y are cousins, otherwise output False. The output should be written to standard output.

## sample
1 2 3 4 null null 5
4 5
True