240
笔下虽有千言,胸中实无一策

30 Day Challenge Day 18 | Leetcode199. Binary Tree Right Side View

题解

Medium

Tree + BFS

class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {
        if(!root) return {};
        
        vector<int> ret;
        
        queue<TreeNode*> q;
        q.push(root);
        
        while(!q.empty()) {
            int sz = q.size();
            for(int i = 0; i < sz; i++) {
                auto t = q.front();
                q.pop();
                if(i == sz-1) ret.push_back(t->val);
                if(t->left) q.push(t->left);
                if(t->right) q.push(t->right);
            }
        }
        
        return ret;
    }
};
posted @ 2020-10-03 05:01  CasperWin  阅读(47)  评论(0编辑  收藏  举报