剑指offer-把二叉树打印成多行
/*从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。*/ import java.awt.List; import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } public class Solution { ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) { ArrayList<ArrayList<Integer>> ret = new ArrayList<>(); Queue<TreeNode> queue = new LinkedList<>(); queue.add(pRoot); while(!queue.isEmpty()) { ArrayList<Integer> list = new ArrayList<>(); int cnt = queue.size(); while(cnt-->0) { TreeNode t = queue.poll(); if(t==null) continue; list.add(t.val); queue.add(t.left); queue.add(t.right); } if(list.size()!=0) ret.add(list); } return ret; } }