package leetcode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * https://leetcode.com/problems/trim-a-binary-search-tree/
 */
public class Problem669 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }

        @Override
        public String toString() {
            return "" + val;
        }
    }

    public TreeNode trimBST(TreeNode root, int L, int R) {
        List<Integer> list = new ArrayList<>();
        traverse(root, list);
        int lidx = Collections.binarySearch(list, L);
        if (lidx < 0) {
            lidx = -(lidx);
        }
        int ridx = Collections.binarySearch(list, R);
        if (ridx < 0) {
            ridx = -(ridx) - 2;
        }
        List<Integer> subList = list.subList(lidx, ridx + 1);
        TreeNode result = null;
        for (int i = 0; i < subList.size(); i++) {
            result = build(result, subList.get(i));
        }
        return result;
    }

    private static TreeNode build(TreeNode node, int val) {
        if (node == null) {
            return new TreeNode(val);
        }
        if (val <= node.val) {
            node.left = build(node.left, val);
        } else {
            node.right = build(node.right, val);
        }
        return node;
    }

    private static void traverse(TreeNode root, List<Integer> list) {
        if (root == null) {
            return;
        }
        traverse(root.left, list);
        list.add(root.val);
        traverse(root.right, list);
    }

    public static void main(String[] args) {
        Problem669 prob = new Problem669();

        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(0);
        root.right = new TreeNode(2);
        TreeNode treeNode = prob.trimBST(root, 1, 2);
        System.out.println(treeNode);

        root = new TreeNode(3);
        root.left = new TreeNode(0);
        root.left.right = new TreeNode(2);
        root.left.right.left = new TreeNode(1);
        root.right = new TreeNode(4);
        treeNode = prob.trimBST(root, 1, 3);
        System.out.println(treeNode);
    }
}

@@ -16,90 +16,8 @@
 e;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.Collections;%0Aimport java.util.List;%0A%0A
 /**%0A
@@ -434,714 +434,105 @@
     
-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
+if (root == null) %7B%0A            return null;%0A        %7D%0A        if (L %3C= root.val && root.val %3C= R
 ) %7B%0A
@@ -547,20 +547,20 @@
     
-node
+root
 .left = 
 buil
@@ -559,28 +559,31 @@
 t = 
-build(node
+trimBST(root
 .left, 
-val
+L, R
 );%0A 
@@ -593,60 +593,45 @@
     
-%7D else %7B%0A            node.right = build(node
+    root.right = trimBST(root
 .right, 
 val)
@@ -626,19 +626,20 @@
 .right, 
-val
+L, R
 );%0A     
@@ -655,235 +655,54 @@
     
-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
+if (root.val %3C L) %7B%0A            return trimBST
 (roo
@@ -714,600 +714,127 @@
 ht, 
-list
+L, R
 );%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)
+%7D%0A        if (root.val %3E R) %7B%0A            return trimBST(root.left, L, R);%0A        %7D%0A        return root
 ;%0A  

Solve problem 669