标签:init new size 层序遍历 efi tps == com add
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
Queue<TreeNode> q = new LinkedList<>();
q.add(root);
List<List<Integer>> ans = new ArrayList<>();
if (root == null) return ans;
while (!q.isEmpty()) {
List<Integer> l = new ArrayList<>();
int sz = q.size();
for (int i = 0; i < sz; ++i) {
TreeNode t = q.poll();
l.add(t.val);
if (t.left != null) {
q.add(t.left);
}
if (t.right != null) {
q.add(t.right);
}
}
ans.add(l);
}
return ans;
}
}
标签:init new size 层序遍历 efi tps == com add
原文地址:https://www.cnblogs.com/clown9804/p/12639107.html