669. 修剪二叉搜索树

根据根节点的值和区间进行对比,来判断递归哪个子树,

  1. class Solution {
  2. public:
  3. TreeNode* trimBST(TreeNode* root, int low, int high) {
  4. if(root==NULL)return root;
  5. if(root->val < low)return trimBST(root->right,low,high);
  6. else if(root->val >high)return trimBST(root->left,low,high);
  7. else{
  8. if(root->left)root->left = trimBST(root->left,low,high);
  9. if(root->right)root->right = trimBST(root->right,low,high);
  10. }
  11. return root;
  12. }
  13. };