Solution in C using recursion


  • 0
    struct TreeNode* trimBST(struct TreeNode* root, int L, int R) {
        bool change = 0;
        if(root) {
            if(root->val < L) {
                root = root->right;
                change = 1;
            } else if (root->val > R) {
                root = root->left;
                change = 1;
            }
        
            if (change) {
                root = trimBST(root, L, R);
            }
            
            if (root) {            
                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.