Short & clean answer in C++


  • 0
    F
       vector<int> rightSideView(TreeNode* root) {
            vector<int> retVal;
            
            rightSideView(root, retVal, 0);
            return retVal;
        }
        
        void rightSideView(TreeNode* root, vector<int>& v, int depth) {
            if (root == NULL) { return; }
            
            for (int i = v.size(); i <= depth; i++)
                v.push_back(0);
    
            v[depth] = root->val;
            
            rightSideView(root->left, v, depth + 1);
            rightSideView(root->right, v, depth + 1);
        }

Log in to reply
 

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