public void printTree(TreeNode top) {
        if (top == null) System.out.print("null.\n");

        ArrayList<TreeNode> treeList = new ArrayList<TreeNode>();
        treeList.add(top);

        while(treeList.size() > 0) {
            ArrayList<TreeNode> tmpList = new ArrayList<TreeNode>();
            for(TreeNode s : treeList) {
                System.out.print(s.val + " ");
                if (s.left != null) tmpList.add(s.left);
                if (s.right != null) tmpList.add(s.right);
            }
            System.out.print("\n");
            treeList = tmpList;
        }
    }