Straightforward C++ Solution


  • 1
    vector<int> rightSideView(TreeNode* root) {
        vector<int> ans;
        if( !root ) return ans;
        vector<TreeNode*> buf;
        buf.push_back(root);
        while( !buf.empty() ) {
            vector<TreeNode*> tmp;
            ans.push_back(buf.back()->val);
            for( TreeNode* t : buf ) {
                if( t && t->left ) tmp.push_back(t->left);
                if( t && t->right ) tmp.push_back(t->right);
            }
            buf = tmp;
        }
        return ans;
    }

Log in to reply
 

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