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>> res = new ArrayList<>();
if(pRoot==null) return res;
Queue<TreeNode> queue = new LinkedList<>();
queue.add(pRoot);
while(!queue.isEmpty()){
ArrayList<Integer> list = new ArrayList<>();
// res.add(list);
int size = queue.size();
while(size!=0){
TreeNode node = queue.poll();
size--;
list.add(node.val);
if(node.left!=null){
queue.add(node.left);
}
if(node.right!=null){
queue.add(node.right);
}
}
res.add(list);
}
return res;
}
}
jz78 将二叉树打印成多行
最新推荐文章于 2024-08-16 16:00:00 发布