#C7989. Rightmost Tree Values
Rightmost Tree Values
Rightmost Tree Values
Given a binary tree, determine the rightmost node's value at each level when the tree is viewed from the right side. More formally, if a binary tree has (n) levels, output an array ( [r_0, r_1, \dots, r_{n-1}] ) where ( r_i ) is the value of the rightmost node at depth (i).
The binary tree is provided in level order traversal where missing nodes are represented by the string null
. Your program should read from standard input and write the correct output to standard output.
inputFormat
A single line containing space-separated tokens representing nodes of the binary tree in level order. Each token is either an integer or the string null
(without quotes) to indicate the absence of a node. For example, the input 1 2 3 null 5 null 4
corresponds to the binary tree used in the example.
outputFormat
Output a single line containing the rightmost node values at each level, separated by a single space. For instance, for the sample input, the expected output is 1 3 4
.## sample
1
1