Get best answers to any doubt/query/question related to programming , jobs, gate, internships and tech-companies. Feel free to ask a question and you will receive the best advice/suggestion related to anything you ask about software-engineering , development and programming problems .

0 like 0 dislike
1,684 views
in Online Assessments by Expert (44,360 points)

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 (44,360 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);  
}
...