#K16495. Find Cousins in a Binary Tree

    ID: 24561 Type: Default 1000ms 256MiB

Find Cousins in a Binary Tree

Find Cousins in a Binary Tree

Given a binary tree, find all the cousins of a given target node. In a binary tree, two nodes are considered cousins if they are at the same depth but have different parents. The output should list these cousin nodes sorted in ascending order.

The input consists of two lines. The first line is a space‐separated string representing the level order traversal of the tree, where the string "null" is used to represent missing children. The second line is an integer representing the target node's value. If the target node does not exist or has no cousins, output an empty line.

Note: All cousin values should be printed in one line separated by a single space.

inputFormat

The first line contains a space-separated list of node values in level order (use "null" for missing nodes). The second line contains an integer target.

outputFormat

Print the cousin nodes of the target in ascending order separated by spaces. If there are no cousins, print an empty line.## sample

1 2 3 4 6 5 8
4
5 8

</p>