Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
956 B
1
Indexable
Never
/**
 * 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) {
        deque<TreeNode*> q;
        q.push_back(root);
        vector<int> res;
        if(root==NULL) return res;
        while(!q.empty()){
            TreeNode* tmp =q.back();
            res.push_back(tmp->val);

            int s=q.size();
            while(s--){
                TreeNode* f=q.front();
                q.pop_front();
                if(f->left)  q.push_back(f->left);
                if(f->right) q.push_back(f->right);
            }
        }
        return res;
    }
};