import java.util.*;
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
ArrayList<ArrayList<Integer>> answer=new ArrayList<>();
if(pRoot==null){
return answer;
}
Queue<TreeNode> queue=new LinkedList<>();
queue.add(pRoot);
while(!queue.isEmpty()){
int size=queue.size();
ArrayList<Integer> tempAnswer=new ArrayList<>();
for(int i=0;i<size;++i){
TreeNode temp=queue.poll();
tempAnswer.add(temp.val);
if(temp.left!=null){
queue.offer(temp.left);
}
if(temp.right!=null){
queue.offer(temp.right);
}
}
answer.add(tempAnswer);
}
return answer;
}
}
07-14
225
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-14
343
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-14
276
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交