@@ -0,0 +1,2202 @@
+package leetcode;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Collections;%0Aimport java.util.List;%0A%0A/**%0A * https://leetcode.com/problems/trim-a-binary-search-tree/%0A */%0Apublic class Problem669 %7B%0A    public static class TreeNode %7B%0A        int val;%0A        TreeNode left;%0A        TreeNode right;%0A%0A        TreeNode(int x) %7B%0A            val = x;%0A        %7D%0A%0A        @Override%0A        public String toString() %7B%0A            return %22%22 + val;%0A        %7D%0A    %7D%0A%0A    public TreeNode trimBST(TreeNode root, int L, int R) %7B%0A        List%3CInteger%3E list = new ArrayList%3C%3E();%0A        traverse(root, list);%0A        int lidx = Collections.binarySearch(list, L);%0A        if (lidx %3C 0) %7B%0A            lidx = -(lidx);%0A        %7D%0A        int ridx = Collections.binarySearch(list, R);%0A        if (ridx %3C 0) %7B%0A            ridx = -(ridx) - 2;%0A        %7D%0A        List%3CInteger%3E subList = list.subList(lidx, ridx + 1);%0A        TreeNode result = null;%0A        for (int i = 0; i %3C subList.size(); i++) %7B%0A            result = build(result, subList.get(i));%0A        %7D%0A        return result;%0A    %7D%0A%0A    private static TreeNode build(TreeNode node, int val) %7B%0A        if (node == null) %7B%0A            return new TreeNode(val);%0A        %7D%0A        if (val %3C= node.val) %7B%0A            node.left = build(node.left, val);%0A        %7D else %7B%0A            node.right = build(node.right, val);%0A        %7D%0A        return node;%0A    %7D%0A%0A    private static void traverse(TreeNode root, List%3CInteger%3E list) %7B%0A        if (root == null) %7B%0A            return;%0A        %7D%0A        traverse(root.left, list);%0A        list.add(root.val);%0A        traverse(root.right, list);%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem669 prob = new Problem669();%0A%0A        TreeNode root = new TreeNode(1);%0A        root.left = new TreeNode(0);%0A        root.right = new TreeNode(2);%0A        TreeNode treeNode = prob.trimBST(root, 1, 2);%0A        System.out.println(treeNode);%0A%0A        root = new TreeNode(3);%0A        root.left = new TreeNode(0);%0A        root.left.right = new TreeNode(2);%0A        root.left.right.left = new TreeNode(1);%0A        root.right = new TreeNode(4);%0A        treeNode = prob.trimBST(root, 1, 3);%0A        System.out.println(treeNode);%0A    %7D%0A%7D%0A

Update problem 669