Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
764 views
in Online Assessments by Expert (46,090 points) | 764 views

1 Answer

0 like 0 dislike
Given a BST and two integers L and R. You have to remove all the nodes from BST whose value lies in [L, R] without using extra space and the properties of BST will remain holds.
by Expert (46,090 points)
0 0
TreeNode* trimBST(TreeNode* root, int l, int r) {        
    if(root==NULL)
        return NULL;
    
    if(root->val>=l && root->val<=r){
        root->left=trimBST(root->left, l,r);
        root->right=trimBST(root->right, l,r);
        return root;
    }else if(root->val <l){
        return trimBST(root->right, l,r);
    }else
        return trimBST(root->left, l,r);  
}