C++ simple and short 8-lines O(n) solution


  • 0
    M
        TreeNode* trimBST(TreeNode* root, int L, int R) {
            if(root==NULL) return NULL;
            if(root->val>R) return trimBST(root->left, L, R);
            else if(root->val<L) return trimBST(root->right, L, R);
            else {
                TreeNode* res=new TreeNode(root->val);
                res->left=trimBST(root->left, L, R);
                res->right=trimBST(root->right, L, R);
                return res;
            }
        }

Log in to reply
 

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