669. 修剪二叉搜索树
分三种情况
- root.val < low ,则递归其右子树节点(因为左子树的值只会比root.val还要小)
- root.val > high 递归其左子树节点
- root.val 符合保存条件,则继续递归其左右子树
class Solution {
public TreeNode trimBST(TreeNode root, int low, int high) {
if(root == null)return null;
if(root.val < low){
return trimBST(root.right,low,high);
}
if(root.val > high){
return trimBST(root.left , low , high);
}
root.left = trimBST(root.left, low , high);
root.right = trimBST(root.right, low, high);
return root;
}
}