class Solution {
private class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
ArrayList<TreeNode> ele = new ArrayList<TreeNode>();
ele.add(pRoot);
if(pRoot==null)
return result;
while(!ele.isEmpty()){
ArrayList<Integer> temp = new ArrayList<Integer>();
int len = ele.size();
for(int i = 0;i<len;i++){
TreeNode node = ele.get(0);
if(node.left!=null)
ele.add(node.left);
if(node.right!=null)
ele.add(node.right);
temp.add(node.val);
ele.remove(0);
}
result.add(temp);
}
return result;
}
}
按层打印二叉树
最新推荐文章于 2019-07-11 23:07:08 发布