/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */classSolution{publicint[]levelOrder(TreeNoderoot){if(root==null){returnnewint[0];}List<Integer>list=newArrayList<>();Queue<TreeNode>queue=newLinkedList<>();queue.offer(root);while(!queue.isEmpty()){intsize=queue.size();for(inti=0;i<size;i++){TreeNodenode=queue.poll();list.add(node.val);if(node.left!=null){queue.offer(node.left);}if(node.right!=null){queue.offer(node.right);}}}int[]arr=newint[list.size()];for(inti=0;i<arr.length;i++){arr[i]=list.get(i);}returnarr;}}