简单题目,BST的搜索遍历
/**
* 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 root;
while(root!=NULL&&((root->val)<L||(root->val)>R)){
if((root->val)<L)
root = root-> right;
else if((root->val)>R)
root = root-> left;
else
{}
}
if (root==NULL)
return NULL;
else{
root->left = trimBST(root->left, L,R);
root->right = trimBST(root->right, L, R);
return root;
}
}
};