您的位置:首页 > 其它

Leetcode 199. 二叉树的右视图

2018-07-22 11:17 295 查看
/**
* 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) {
if(root==NULL)
return vector<int>();
vector<int> ret;
queue<TreeNode*> q;
q.push(root);
while(!q.empty())
{
int size = q.size();
ret.push_back(q.back()->val);
for(int i=0; i<size; ++i)
{
auto tmp = q.front();
q.pop();
if(tmp->left)
{
q.push(tmp->left);
}
if(tmp->right)
{
q.push(tmp->right);
}
}
}
return ret;
}
};

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐