import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
/*
* 剑指 Offer 32 - I. 从上到下打印二叉树
*/
class Solution {
Queue<TreeNode> queue = new LinkedList<>();
ArrayList<Integer> list = new ArrayList<>();
public int[] levelOrder(TreeNode root) {
TreeNode node;
if (root == null) {
return hello(list);
}
queue.add(root);
while (!queue.isEmpty()) {
node = queue.poll();
list.add(node.val);
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
return hello(list);
}
public int[] hello(List list) {
int size = list.size();
int[] array = new int[size];
for (int i = 0; i <= size - 1; i++) {
array[i] = (Integer)list.get(i);
}
return array;
}
}
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}