Skip to content

Latest commit

 

History

History
29 lines (28 loc) · 778 Bytes

89.md

File metadata and controls

29 lines (28 loc) · 778 Bytes

Binary Tree Right Side View

LeetCode Link

    vector<int> rightSideView(TreeNode* root) {
        vector<int> ans;
        if(!root){
            return ans;
        }
        queue<TreeNode*>q;
        q.push(root);
        while(!q.empty()){
            int size=q.size();
            for(int i=0;i<size;i++){
                TreeNode* temp=q.front();
                q.pop();
                if(i==size-1){
                    ans.push_back(temp->val);
                }
                if(temp->left){
                    q.push(temp->left);
                }
                if(temp->right){
                    q.push(temp->right);
                }
            }
        }

        return ans;
    }