输出每一层的最大值
/************ 只打印每一层的最大节点 ************/
public void printBiggest(Queue<Twotree> queue){
Iterator iterator=queue.iterator();
int max=Integer.MIN_VALUE;
while(iterator.hasNext()){
Twotree note=(Twotree)iterator.next();
if (note.val>max){
max=note.val;
}
}
System.out.println(max);
}
public void printBiggestInPile(Twotree head){
int temp1=1; //未打印节点数
int temp2=0; //总共要打印节点数
Queue<Twotree> queue=new ArrayDeque<>();
if (head==null){
throw new RuntimeException("input can't be null");
}
queue.offer(head);
printBiggest(queue); //打印第一层
while(queue.size()>0){
Twotree note=queue.poll();
temp1-=1;
if (note.left!=null){
queue.offer(note.left);
temp2+=1;
}
if (note.right!=null) {
queue.offer(note.right);
temp2 += 1;
}
if (temp1==0&&temp2!=0){
temp1=temp2;
temp2=0;
printBiggest(queue);
}
}
}