/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
if(!root)
return {};
queue<TreeNode*> now,next;
now.push(root);
vector<int> ans;
while(!now.empty()){
TreeNode* front=NULL;
while(!now.empty()){
front=now.front();
now.pop();
if(front->left){
next.push(front->left);
}
if(front->right){
next.push(front->right);
}
}
ans.push_back(front->val);
swap(now,next);
}
return ans;
}
};
199. 二叉树的右视图
最新推荐文章于 2023-03-17 15:57:46 发布