C++ solution 6 lines


  • -1

    My idea is easy,like greedy in some way.
    what I do is to make sure root val is in [L,R]

    class Solution {
    public:
    TreeNode* trimBST(TreeNode* root, int L, int R) {
    while(root&&(root->val<L||root->val>R))root=root->val<L?root->right:root->left;
    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.