#K14146. Print Level in Binary Tree
Print Level in Binary Tree
Print Level in Binary Tree
You are given a binary tree and a target integer. Your task is to print all the node values that lie on the same level as the target node in the binary tree. If the target node is not present in the tree, output nothing.
The binary tree is provided in level order where the value null
denotes that there is no node present in that position. The level of the root is considered to be 1. For example, consider the binary tree below:
If the target is 5
, the output should consist of the values on the level containing node 5
, which is 4 5 6 7
.
Note: All outputs should be printed in a single line, with values separated by a single space. If the target node is not in the tree, print nothing.
inputFormat
The input is given via standard input (stdin) and consists of two lines:
- The first line contains the level order traversal of the binary tree. Node values are separated by spaces. The token
null
represents a missing node. - The second line contains an integer, the target value.
It is guaranteed that the tree contains at least one node.
outputFormat
Print the values of all nodes located on the same level as the target node in left-to-right order. The node values should be separated by a single space. If the target node does not exist in the tree, output nothing.
## sample1 2 3 4 5 6 7 8 null null null null null null null
5
4 5 6 7