Can anyone share an clean iterative solution?


  • 0
    J

    I saw most of us complete this problem by a simple recursion, and the answers are quite similar to each other, for example, my recursion sol:

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

    But in an real interview, I was asked to finish this problem without recursion as a follow-up.
    At that time, what I did was starting from a normal iterative version of post-order traverse and tried to build my solution on top of that, which leads to a very messy page of codes and the interviewer seems to be unsatisfied with my answer.
    So can anybody share an clean iterative solution?


Log in to reply
 

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