c++ solution


  • 0
    B
    class Solution {
    public:
        vector<int> result;
        
        void dfs(TreeNode *root, int h){
            if(!root) return ;
            if(h>=result.size()) result.push_back(root->val);
            else result[h]=root->val;
            
            if(root->left) dfs(root->left, h+1);
            if(root->right) dfs(root->right, h+1);
            
        }
        
        vector<int> rightSideView(TreeNode* root) {
            dfs(root, 0);
            return result;
        }
    };
    

Log in to reply
 

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