# 11.完全二叉树的判定

import java.util.LinkedList;
import java.util.Queue;

public class TestComplateBinaryTree{

public static class BiTree{
public int value;
public  BiTree left;
public  BiTree right;
public  BiTree(int data){
this.value = data ;
}

}

public static boolean isCBT(BiTree head){
if(head ==  null) return false;
//使用链队列  实现
Queue<BiTree> queue = new  LinkedList<BiTree>();
boolean leaf =false;  //判断该节点之后 都是叶子
BiTree l = null;
BiTree r =null;
queue.offer(head);  //加入队列
while(!queue.isEmpty()){
head = queue.poll();
l = head.left;
r =head.right;
if((l==null)&&(r!=null) || leaf&&(l!=null||r!=null)) {
//左子树 空  右子树 不空  不是完全
//左右不是都有，那之后都是叶子
return false;
}

if(l!=null) queue.offer(l);
if(r!=null) queue.offer(r);
else {
leaf =true;
}

}

return true ;

}

public static void main(String []args){
BiTree head= new BiTree(4);
head.left = new BiTree(2);
head.right = new BiTree(6);
head.left.left = new BiTree(1);
head.left.right = new BiTree(3);
head.right.left = new BiTree(5);
boolean f=  isCBT( head) ;

System.out.println(f);
}

}
01-26 134
05-01 394

11-19 4639
11-13 1万+
02-27 305
03-01 68
06-09 549
06-12 789
10-30 280
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试