#K81127. Right Side View of a Binary Tree
Right Side View of a Binary Tree
Right Side View of a Binary Tree
Given the root of a binary tree, your task is to return a list of values of the nodes that are visible when the tree is viewed from the right side.
You will be provided with the level order traversal of the binary tree where non-existent nodes are represented by the string null
. The right side view of the tree corresponds to the rightmost node at each level.
The input will be read from standard input (stdin), and the output (the right side view) must be printed to standard output (stdout) as a space-separated list of integer values on one line.
The formula for determining the index of the children in a 0-indexed array representation is given by:
$$left = 2i+1, \quad right = 2i+2,$$
where i is the index of the current node.
inputFormat
The input consists of a single line containing space-separated tokens representing the level order traversal of a binary tree. Each token is either an integer or the string null
which denotes a missing node. For example:1 2 3 null 5 null 4
outputFormat
Output the right side view of the binary tree as a space-separated sequence of integers. There should be no extra spaces at the beginning or at the end of the output.
## sample1 2 3 null 5 null 4
1 3 4