[leetcode]669. Trim a Binary Search Tree寻找范围内的二叉搜索树

根据BST的特点,如果小于L就判断右子树,如果大于R就判断左子树

递归地建立树

public TreeNode trimBST(TreeNode root, int L, int R) {
        if (root==null) return null;
        if (root.val<L) return trimBST(root.right,L,R);
        if (root.val>R) return trimBST(root.left,L,R);
        TreeNode res = new TreeNode(root.val);
        res.left = trimBST(root.left,L,R);
        res.right = trimBST(root.right,L,R);
        return res;
    }
原文地址:https://www.cnblogs.com/stAr-1/p/8410452.html