package org.hfeng.leet.util;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import static org.junit.Assert.assertEquals;

public class TreeNodeHelper {
    public static TreeNode createListFromArray(int[] array) {
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        if (array.length == 0) {
            return null;
        }
        TreeNode root = new TreeNode(array[0]);
        queue.offer(root);

        int count = 0;
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if (++count < array.length) {
                cur.left = array[count] == -1 ? null : new TreeNode(array[count]);
                queue.offer(cur.left);
            } else {
                cur.left = null;
            }
            if (++count < array.length) {
                cur.right = array[count] == -1 ? null : new TreeNode(array[count]);
                queue.offer(cur.right);
            } else {
                cur.right = null;
            }
        }
        return root;
    }

    public static void assertEqualTree(TreeNode left, TreeNode right) {
        assertEquals(preOrderTree(left), preOrderTree(right));
    }

    public static List<Integer> preOrderTree(TreeNode root) {
        List<Integer> ret = new ArrayList<Integer>();
        preOrderSub(root, ret);
        return ret;
    }

    private static void preOrderSub(TreeNode root, List<Integer> ret) {
        if (root == null) {
            return;
        }
        ret.add(root.val);
        if (root.left != null) {
            preOrderTree(root.left);
        }

        if (root.right != null) {
            preOrderTree(root.right);
        }
    }
}

@@ -23,16 +23,102 @@
 .util;%0A%0A
+import org.hamcrest.collection.IsIterableContainingInOrder;%0Aimport org.junit.Assert;%0A%0A
 import j
@@ -133,24 +133,24 @@
 .ArrayList;%0A
-
 import java.
@@ -1244,59 +1244,652 @@
     
-assertEquals(preOrderTree(left), preOrderTree(right
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////%0A        // From                                                                                                                     //%0A        // http://stackoverflow.com/questions/12495420/how-to-junit-test-that-two-liste-contain-the-same-elements-in-the-same-order //%0A        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////%0A        Assert.assertThat(preOrderTree(right), IsIterableContainingInOrder.contains(preOrderTree(left).toArray()
 ));%0A
@@ -2276,28 +2276,27 @@
     preOrder
-Tree
+Sub
 (root.left);
@@ -2289,24 +2289,29 @@
 ub(root.left
+, ret
 );%0A        %7D
@@ -2366,20 +2366,19 @@
 preOrder
-Tree
+Sub
 (root.ri
@@ -2376,24 +2376,29 @@
 b(root.right
+, ret
 );%0A        %7D

Correct the helper function, need testcase for helper later