import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * } */ public class Solution { /** * * @param root TreeNode类 * @return int整型ArrayList<ArrayList<>> */ public ArrayList<ArrayList<Integer>> levelOrder (TreeNode root) { // write code here ArrayList<ArrayList<Integer>> list = new ArrayList<>(); if(root == null) return list; Queue<TreeNode> queue = new LinkedList<>(); queue.add(root); while(!queue.isEmpty()){ ArrayList<Integer> line = new ArrayList<>(); int size = queue.size(); for(int i = 0; i < size; ++i){ TreeNode node = queue.poll(); line.add(node.val); if(node.left != null){queue.add(node.left);} if(node.right != null){queue.add(node.right);} } list.add(line); } return list; } }