import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* }
*/
public class Solution {
/**
*
* @param root TreeNode类
* @return int整型ArrayList<ArrayList<>>
*/
public ArrayList<ArrayList<Integer>> levelOrder (TreeNode root) {
// write code here
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
if(root == null)
return res;
ArrayList<Integer> list;
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.offer(root);
while(!q.isEmpty()) {
int size = q.size();
list = new ArrayList<Integer>();
for(int i = 0; i < size; i++) {
TreeNode t = q.poll();
list.add(t.val);
if(t.left != null) {
q.offer(t.left);
}
if(t.right != null) {
q.offer(t.right);
}
}
res.add(list);
}
return res;
}
}
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* }
*/
public class Solution {
/**
*
* @param root TreeNode类
* @return int整型ArrayList<ArrayList<>>
*/
public ArrayList<ArrayList<Integer>> levelOrder (TreeNode root) {
// write code here
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
if(root == null)
return res;
ArrayList<Integer> list;
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.offer(root);
while(!q.isEmpty()) {
int size = q.size();
list = new ArrayList<Integer>();
for(int i = 0; i < size; i++) {
TreeNode t = q.poll();
list.add(t.val);
if(t.left != null) {
q.offer(t.left);
}
if(t.right != null) {
q.offer(t.right);
}
}
res.add(list);
}
return res;
}
}