水题 切了
package pro102;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
/**
* @Author: qiqu
*/
public class Solution {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public List<List<Integer>> levelOrder(TreeNode root) {
Queue<TreeNode> q = new LinkedList<>();
List<List<Integer>> result = new LinkedList<>();
if(root == null){
return result;
}
List<Integer> list1 = new LinkedList<>();
list1.add(root.val);
result.add(list1);
int cnt=0;
if(root.left != null){
cnt++;
q.offer(root.left);
}
if(root.right != null){
cnt++;
q.offer(root.right);
}
while(!q.isEmpty()){
List<Integer> list = new LinkedList<>();
int tmp = 0;
for(int i=0;i<cnt;i++){
TreeNode node = q.poll();
list.add(node.val);
if(node.left != null){
tmp++;
q.offer(node.left);
}
if(node.right != null){
tmp++;
q.offer(node.right);
}
}
cnt=tmp;
result.add(list);
}
return result;
}
}