6-liner Recursion

  • 0

    Without releasing memory

        TreeNode* trimBST(TreeNode* r, int L, int R) {
            if (r) {
              if (r->val < L) return trimBST(r->right, L, R);
              if (r->val > R) return trimBST(r->left,  L, R);
              r->left  = trimBST(r->left,  L, R);
              r->right = trimBST(r->right, L, R);
            return r;

Log in to reply

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