package leetcode;

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

/**
 * https://leetcode.com/problems/unique-binary-search-trees-ii/
 */
public class Problem95 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) {
            val = x;
        }

        @Override
        public String toString() {
            return Integer.toString(val);
        }
    }

    public List<TreeNode> generateTrees(int n) {
        List<TreeNode> result = new ArrayList<>();
        for (int i = 1; i <= n; i++) {
            TreeNode root = new TreeNode(i);
            List<Integer> numbers = new ArrayList<>();
            for (int j = 1; j <= n; j++) {
                if (i == j) {
                    continue;
                }
                numbers.add(j);
            }
            generate(numbers, root, result);
        }
        return result;
    }

    private void generate(List<Integer> numbers, TreeNode root, List<TreeNode> result) {
        if (numbers.size() == 0) {
            System.out.println("hey");
            result.add(root);
            return;
        }
        for (int i = 0; i < numbers.size(); i++) {
            int val = numbers.get(i);
            TreeNode c = copy(root);
            TreeNode r = add(c, val);
            List<Integer> newNumbers = new ArrayList<>();
            for (int j = 0; j < numbers.size(); j++) {
                if (i == j) {
                    continue;
                }
                newNumbers.add(numbers.get(j));
            }
            generate(newNumbers, r, result);
        }
    }

    private TreeNode copy(TreeNode node) {
        TreeNode cp = new TreeNode(node.val);
        copy(node, cp);
        return cp;
    }

    private void copy(TreeNode src, TreeNode dest) {
        if (src == null) {
            return;
        }
        if (src.left != null) {
            TreeNode newNode = new TreeNode(src.left.val);
            dest.left = newNode;
            copy(src.left, newNode);
        }
        if (src.right != null) {
            TreeNode newNode = new TreeNode(src.right.val);
            dest.right = newNode;
            copy(src.right, newNode);
        }
    }

    private TreeNode add(TreeNode node, int val) {
        if (node == null) {
            return new TreeNode(val);
        }
        if (node.val > val) { // left
            node.left = add(node.left, val);
        } else { // right
            node.right = add(node.right, val);
        }
        return node;
    }

    public static void main(String[] args) {
        Problem95 prob = new Problem95();
        List<TreeNode> result = prob.generateTrees(3);
        for (TreeNode node : result) {
            System.out.println(node);
        }
//        TreeNode root = prob.add(null, 1);
//        root = prob.add(root, 3);
//        root = prob.add(root, 2);
//
//        System.out.println(root);
//        System.out.println(root.right);
//        System.out.println(root.right.left);
//
//        TreeNode copy = prob.copy(root);
//
//        System.out.println(copy);
//        System.out.println(copy.right);
//        System.out.println(copy.right.left);
    }
}

@@ -57,19 +57,120 @@
 va.util.
-Lis
+HashSet;%0Aimport java.util.LinkedList;%0Aimport java.util.List;%0Aimport java.util.Queue;%0Aimport java.util.Se
 t;%0A%0A/**%0A
@@ -421,114 +421,8 @@
    %7D
-%0A%0A        @Override%0A        public String toString() %7B%0A            return Integer.toString(val);%0A        %7D
 %0A   
@@ -517,32 +517,143 @@
  ArrayList%3C%3E();%0A
+        if (n == 0) %7B%0A            result.add(null);%0A        %7D%0A        Set%3CString%3E treeNodes = new HashSet%3C%3E();%0A
         for (int
@@ -977,16 +977,27 @@
 s, root,
+ treeNodes,
  result)
@@ -1101,16 +1101,65 @@
 de root,
+ Set%3CString%3E treeNodes,%0A                         
  List%3CTr
@@ -1228,61 +1228,157 @@
    S
-ystem.out.println(%22hey%22);%0A            result.add(root
+tring str = toString(root);%0A            if (!treeNodes.contains(str)) %7B%0A                result.add(root);%0A            %7D%0A            treeNodes.add(str
 );%0A 
@@ -1858,16 +1858,27 @@
 bers, r,
+ treeNodes,
  result)
@@ -2828,86 +2828,92 @@
    p
-ublic static void main(String%5B%5D args) %7B%0A        Problem95 prob = new Problem95
+rivate String toString(TreeNode node) %7B%0A        StringBuilder sb = new StringBuilder
 ();%0A
@@ -2912,36 +2912,37 @@
 lder();%0A        
-List
+Queue
 %3CTreeNode%3E resul
@@ -2940,124 +2940,103 @@
 de%3E 
-result = prob.generateTrees(3);%0A        for (TreeNode node : result) %7B%0A            System.out.println(node);
+queue = new LinkedList%3C%3E();%0A        queue.add(node);%0A        while (!queue.isEmpty()) %7B
 %0A       
  %7D%0A/
@@ -3035,21 +3035,13 @@
     
- %7D%0A//
      
-   
 Tree
@@ -3049,44 +3049,36 @@
 ode 
-root = prob.add(null, 1
+n = queue.remove(
 );%0A
-//
         
 root
@@ -3077,44 +3077,44 @@
     
-root = prob.add(root, 3
+    sb.append(n.val + %22,%22
 );%0A
-//
         
 root
@@ -3113,333 +3113,205 @@
     
-root = prob.add(root, 2);%0A//%0A//        System.out.println(root);%0A//        System.out.println(root.right);%0A//        System.out.println(root.right.left);%0A//%0A//        TreeNode copy = prob.copy(root);%0A//%0A//        System.out.println(copy);%0A//        System.out.println(copy.right);%0A//        System.out.println(copy.right.left
+    if (n.left != null) %7B%0A                queue.add(n.left);%0A            %7D%0A            if (n.right != null) %7B%0A                queue.add(n.right);%0A            %7D%0A        %7D%0A        return sb.toString(
 );%0A 

Solve problem 95