5 lines C++


  • 0
    class Solution {
    public:
        TreeNode* trimBST(TreeNode* root, int L, int R) {
            while(root && (root->val < L || root->val > R)) root = root->val < L ? root->right : root->left;
            if(!root) return NULL;
            root->left = trimBST(root->left, L, R);
            root->right = trimBST(root->right, L, R);
            return root;
        }
    };
    

Log in to reply
 

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