
class Solution {
public int[] levelOrder(TreeNode root) {
if (root == null) {
return new int[0];
}
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
List<Integer> res = new ArrayList<Integer>();
while (!queue.isEmpty()) {
TreeNode cur = queue.poll();
res.add(cur.val);
if (cur.left != null) {
queue.add(cur.left);
}
if (cur.right != null) {
queue.add(cur.right);
}
}
int[] ans = new int[res.size()];
for (int i = 0; i < ans.length; i++) {
ans[i] = res.get(i);
}
return ans;
}
}
class Solution {
public int[] levelOrder(TreeNode root) {
if (root == null) {
return new int[0];
}
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
List<Integer> res = new ArrayList<Integer>();
while (!queue.isEmpty()) {
TreeNode cur = queue.poll();
res.add(cur.val);
if (cur.left != null) {
queue.add(cur.left);
}
if (cur.right != null) {
queue.add(cur.right);
}
}
return res.stream().mapToInt(Integer::valueOf).toArray();
}
}