其实就是返回符合的根节点嘛,树都一个德行全是递归
/**
* 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) return NULL;
root = deal(root,L,R);
if(root)
{
root->left = trimBST(root->left,L,R);
root->right = trimBST(root->right,L,R);
}
return root;
}
TreeNode* deal(TreeNode* root, int L, int R)
{
if(!root) return NULL;
if(root->val>R)
{
root = root->left;
return deal(root,L,R);
}
if(root->val<L)
{
root = root->right;
return deal(root,L,R);
}
else
{
return root;
}
}
};