作者:灬耗丨子灬 | 来源:互联网 | 2023-10-13 08:38
题解1:
class Solution {
public int[] levelOrder(TreeNode root) {
if(root == null) return new int[0];
Queue queue = new LinkedList<>(){{ add(root); }};
ArrayList ans = new ArrayList<>();
while(!queue.isEmpty()) {
TreeNode node = queue.poll();
ans.add(node.val);
if(node.left != null) queue.add(node.left);
if(node.right != null) queue.add(node.right);
}
int[] res = new int[ans.size()];
for(int i = 0; i res[i] = ans.get(i);
return res;
}
}