剑指Offer_60 发表于 2017-12-06 | 分类于 剑指Offer 题目从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。 解题思路二叉树层序遍历 1234567891011121314151617181920212223242526272829303132333435363738394041424344import java.util.ArrayList;import java.util.LinkedList;/*public 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>> AA = new ArrayList<>(); if(pRoot == null){ return AA; } LinkedList<TreeNode> nodeList = new LinkedList<>(); nodeList.add(pRoot); while(!nodeList.isEmpty()){ ArrayList<Integer> A = new ArrayList<>(); int size = nodeList.size(); for(int i = 0; i < size; ++i){ TreeNode node = nodeList.removeFirst(); A.add(node.val); if(node.left != null){ nodeList.addLast(node.left); } if(node.right != null){ nodeList.addLast(node.right); } } AA.add(A); } return AA; } }