题解 | #把二叉树打印成多行#
把二叉树打印成多行
http://www.nowcoder.com/practice/445c44d982d04483b04a54f298796288
public class Solution { ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) { Queue<TreeNode> q=new LinkedList<TreeNode>(); ArrayList<Integer> arr=new ArrayList<Integer>(); ArrayList<ArrayList<Integer> > arr1=new ArrayList<ArrayList<Integer> >(); if(pRoot==null) return arr1; TreeNode cur=pRoot,last=pRoot;TreeNode tail=pRoot; q.offer(cur); while(!q.isEmpty()) { cur=q.poll(); arr.add(cur.val); if(cur.left!=null) { q.offer(cur.left); tail=cur.left; } if(cur.right!=null) { q.offer(cur.right); tail=cur.right; } if(last==cur) { last=tail; arr1.add(new ArrayList(arr)); arr.clear(); } } return arr1; } }