/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */classSolution{publicList<List<Integer>>levelOrder(TreeNoderoot){List<List<Integer>>levelOrder=newArrayList<List<Integer>>();if(root==null){returnlevelOrder;}Queue<TreeNode>queue=newLinkedList<TreeNode>();queue.offer(root);while(!queue.isEmpty()){List<Integer>level=newArrayList<Integer>();intsize=queue.size();for(inti=0;i<size;i++){TreeNodenode=queue.poll();level.add(node.val);if(node.left!=null){queue.add(node.left);}if(node.right!=null){queue.add(node.right);}}levelOrder.add(level);}returnlevelOrder;}}