LeetCode 102. 二叉树的层序遍历
原题链接
中等
作者:
autumn_0
,
2024-09-28 09:43:09
,
所有人可见
,
阅读 3
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
if(root == null) return List.of();
List<List<Integer>> ans = new ArrayList<>();
Queue<TreeNode> q = new ArrayDeque<>();
q.add(root);
while(!q.isEmpty()){
int n = q.size();
List<Integer> vals = new ArrayList<>(n);
while(n -- > 0){
TreeNode node = q.poll();
vals.add(node.val);
if(node.left != null) q.add(node.left);
if(node.right != null) q.add(node.right);
}
ans.add(vals);
}
return ans;
}
}