二叉树右视图
Problem statement:
问题陈述:
Given a Binary Tree, print Right view of it. Right view of a Binary Tree is set of nodes visible when tree is visited from Right side.
给定一个二叉树,打印它的右视图 。 二叉树的右视图是从右侧访问树时可见的节点集。
Example:
例:
For the above tree:
Output: Right view
2, 5, 9, 4
Solution
解
Right view of a binary tree
二叉树的右视图
Right view of a binary tree means the nodes which are visible when tree is view from right direction only.
二叉树的右视图表示仅从右方向查看树时可见的节点。
The above problem can be solved using level order traversal. For every level traversed, print only the rightmost node.
可以使用级别顺序遍历来解决上述问题。 对于遍历的每个级别,仅打印最右边的节点。
Algorithm
算法
Pre-requisite:
先决条件:
A Queue, input binary tree
队列,输入二叉树
FUNCTION rightVie