カジュアルワーキング
出力層のノードで左から右へ、上から下に層にバイナリツリーを印刷します。それぞれの層の出力ライン
思考
トラバーサル順序うん!!!
コード
public class Solution {
ArrayList<ArrayList<Integer> > Print(TreeNode root) {
Queue<TreeNode> queue=new LinkedList<TreeNode>();
ArrayList<ArrayList<Integer>> arrays=new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> array=new ArrayList<Integer>();
if(root==null)return arrays;
queue.offer(root);
// array.add(root.val); 如何进行表示表示他们是在同一层 是个问题的关键所在 这一点想不出来啊有点 可以利用for循环来实现
while(queue.size()!=0){
int len=queue.size();
for(int i=0;i<len;i++){
TreeNode temp=queue.poll();
if(temp.left!=null){
queue.add(temp.left);
}
if(temp.right!=null){
queue.add(temp.right);
}
array.add(temp.val);
}
arrays.add(array);
array=new ArrayList<Integer>();
}
return arrays;
}
}