package Solutions;
import java.util.ArrayList;
import java.util.ArrayDeque;
import java.util.Iterator;
import java.util.Queue;
/**
* Created by hu on 2015/12/11.
*/
/*
* 把二叉树打印成多行
*从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
* */
public class solution28 {
private static Queue<TreeNode> queue=new ArrayDeque<TreeNode>();
public static ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
//返回的结果
ArrayList<ArrayList<Integer>> arrayLists=new ArrayList<ArrayList<Integer>>();
if(pRoot==null){
return arrayLists;
}
ArrayList<TreeNode> temp=new ArrayList<TreeNode>();
temp.add(pRoot);
while (!temp.isEmpty()){
ArrayList<TreeNode> next=new ArrayList<TreeNode>();
ArrayList<Integer> arrayList=new ArrayList<Integer>();
Iterator<TreeNode> iterator=temp.iterator();
while (iterator.hasNext()){
TreeNode node=iterator.next();
arrayList.add(node.val);
if (node.left!=null){
next.add(node.left);
}
if(node.right!=null){
next.add(node.right);
}
}
temp=next;
arrayLists.add(arrayList);
}
return arrayLists;
}
public static void main(String[] args){
TreeNode h1=new TreeNode(8);
TreeNode h2=new TreeNode(6);
TreeNode h3=new TreeNode(10);
TreeNode h4=new TreeNode(5);
TreeNode h5=new TreeNode(7);
TreeNode h6=new TreeNode(9);
TreeNode h7=new TreeNode(11);
h1.left=h2;
h1.right=h3;
h2.left=h4;
h2.right=h5;
h3.left=h6;
h3.right=h7;
ArrayList<ArrayList<Integer>> arrayLists=Print(h1);
System.out.print(arrayLists);
}
}