Simple level traversal yet accepted as best in cpp


  • 1
    class Solution {
    public:
        vector<int> rightSideView(TreeNode* root) {
            vector<TreeNode*> v0, v1;
            vector<int> v;
            if(!root) return v;
            v0.push_back(root);
            while(v0.size())
            {
                v.push_back(v0.back()->val);
                for(auto& node: v0)
                {
                    if(node->left)
                        v1.push_back(node->left);
                    if(node->right)
                        v1.push_back(node->right);
                }
                v0.swap(v1);
                v1.clear();
            }
            return v;
        }
    };

Log in to reply
 

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