给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
示例:
输入: [1,2,3,null,5,null,4] 输出: [1, 3, 4] 解释: 1 <--- / \ 2 3 <--- \ \ 5 4 <---
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> view;
queue<pair<TreeNode*,int> > Q;
if(root)
Q.push(make_pair(root,0));
while(!Q.empty()){
int depth = Q.front().second;
TreeNode *node = Q.front().first;
Q.pop();
if(view.size()==depth)
view.push_back(node->val);
else
view[depth]=node->val;
if(node->left)
Q.push(make_pair(node->left,depth+1));
if(node->right)
Q.push(make_pair(node->right,depth+1));
}
return view;
}
};