My simple c++ solution --stack


  • 0
    K
    class Solution {
    public:
    vector<int> rightSideView(TreeNode* root) {
        vector<int>res;
        queue<TreeNode*>queue;
        
        TreeNode * temp;
        if(!root)return res;
        queue.push(root);
        while(!queue.empty())
        {
            int size=queue.size();
            while(size>0)
            {
                temp=queue.front();
                queue.pop();
                size--;
                if(temp->left)queue.push(temp->left);
                if(temp->right)queue.push(temp->right);
            }
            res.push_back(temp->val);
        }
        return res;
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.