版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013554860/article/details/83280375
https://leetcode.com/problems/trim-a-binary-search-tree/description/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int L, int R) {
if(root==NULL) return NULL;
if(L > root->val){
return trimBST(root->right, L, R);
}
if(R < root->val){
return trimBST(root->left, L, R);
}
if(L<=root->val && root->val <=R){
root->left = trimBST(root->left, L, R);
root->right = trimBST(root->right, L, R);
return root;
}
}
};