#K36862. Nodes at Given Depth in a Binary Tree
Nodes at Given Depth in a Binary Tree
Nodes at Given Depth in a Binary Tree
You are given a binary tree in level-order format and an integer depth. Your task is to output all the node values located at the given depth in the tree, in left-to-right order.
The binary tree is provided as a sequence of values separated by spaces where null
indicates the absence of a node. The first line represents the level-order traversal of the tree, and the second line contains the target depth (with the root at depth 0).
Note: If there are no nodes at the given depth, output an empty line.
Mathematically, if the root is at depth 0, for any given depth \( d \), output the list \( L_d = [n_1, n_2, \dots, n_k] \) where \( n_i \) are the node values at depth \( d \) from left to right.
inputFormat
The input consists of two lines:
- The first line contains a space-separated list of values representing the binary tree in level-order. Use the string
null
to represent a missing node. - The second line contains a single integer denoting the target depth.
For example:
1 2 3 4 5 6 7 2
outputFormat
Output a single line containing the values of the nodes at the given depth, separated by a single space. If no nodes exist at that depth, output an empty line.
## sample1
0
1