class Solution {
public int[] levelOrder(TreeNode root) {
if(root==null) return new int[0];
Queue<TreeNode> que = new LinkedList<>();
ArrayList<Integer> ans= new ArrayList<>();
que.add(root);
while(!que.isEmpty()){
TreeNode node = que.poll();
ans.add(node.val);
if(node.left!=null) que.add(node.left);
if(node.right!=null) que.add(node.right);
}
int[] res = new int[ans.size()];
int k = 0;
for(int i:ans){
res[k++] = i;
}
return res;
}
}
二叉树层次遍历
最新推荐文章于 2022-05-17 07:15:00 发布