LeetCode 669. 修剪二叉搜索树

//根据根节点的值 与边界的值 进行比较 可得出
// node.val > high,那么修剪后的二叉树必定出现在节点的左边。
// node.val < low,那么修剪后的二叉树出现在节点的右边
// 否则,修剪树的两边。
class Solution {
    public TreeNode trimBST(TreeNode root, int low, int high) {
        if(root == null) return null;
        if(root.val > high) return trimBST(root.left,low,high);
        if(root.val < low) return trimBST(root.right,low,high);
        root.left = trimBST(root.left,low,high);
        root.right = trimBST(root.right,low,high);
        return root;
    }
}
原文地址:https://www.cnblogs.com/peanut-zh/p/13882185.html