#C4470. Right Side View of a Binary Tree
Right Side View of a Binary Tree
Right Side View of a Binary Tree
Given a binary tree, your task is to compute the right side view of the tree. The right side view of a binary tree is the set of nodes visible when the tree is viewed from the right side. More formally, if a binary tree is defined recursively as (node = (val, left, right)), then the right side view consists of the last node at each level when performing a level order traversal (i.e., breadth-first search).
Example:
For the following tree:
1 / \ 2 3 \ \ 5 4
The right side view is: [1, 3, 4].
inputFormat
Input is given as a single line containing space-separated tokens representing the level-order traversal of the binary tree. Each token is either an integer (denoting a node value) or the string 'null' (denoting the absence of a node). The first token represents the root of the tree. For example, the input "1 2 3 null 5 null 4" represents a tree where 1 is the root, 2 and 3 are its children, 2 has a right child 5, and 3 has a right child 4. An input of "null" indicates that the tree is empty.
outputFormat
Output the right side view of the tree as a sequence of integers separated by a single space on one line. If the tree is empty, output an empty line.## sample
1
1
</p>