1.准备节点
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}
2.方法1
/**
* check whether entirely binary tree
* function one
* @param head
* @return
*/
public static boolean isEntirely1(Node head) {
if(head == null){
return true;
}
// level traversal
Queue<Node> queue = new LinkedList<>();
queue.add(head);
// default is not a leaf node
boolean leaf = false;
while (!queue.isEmpty()){
head = queue.poll();
/*
if the node is leaf node
the node left and right must null
if the node not is leaf node
the node left is null , right not null
==> not entirely binary tree
*/
if((leaf && (head.left != null || head.right != null)) || (head.left == null && head.right != null)){
return false;
}
if(head.left != null ){
queue.add(head.left);
}
if(head.right != null ){
queue.add(head.right);
}
// if head.left == null or
// if head.left != null head.right == null
// ==> the next poll node is leaf node
if (head.left == null || head.right == null){
leaf = true;
}
}
return true;
}
2.方法2
2.1准备实体保存该树信息
public static class Info {
private boolean isEntirely;
private boolean isFull;
private int height;
public Info(boolean isE,boolean isF, int h){
isEntirely = isE;
isFull = isF;
height = h;
}
}
2.2具体调用
/**
* check whether entirely binary tree
* function two
* @param head
* @return
*/
public static boolean isEntirely2(Node head) {
if(head == null){
return true;
}
return process(head).isEntirely;
}
public static Info process(Node node) {
if(node == null){
return new Info(true,true,0);
}
Info leftInfo = process(node.left);
Info rightInfo = process(node.right);
// determine the three is full
boolean isFull = leftInfo.isFull && rightInfo.isFull && leftInfo.height == rightInfo.height;
// isEntirely
boolean isEntirely = false;
if(isFull){
isEntirely = true;
}else {
// both sides are complete binary trees
if(leftInfo.isEntirely && rightInfo.isEntirely){
/*
a
/ \
b null
leftInfo.isEntirely
rightInfo.isFull
leftInfo.height == rightInfo.height + 1
*/
if(rightInfo.isFull && leftInfo.height == rightInfo.height + 1){
isEntirely = true;
}
/*
a
/ \
b c
/ \ / \
e f g null
leftInfo.isFull
rightInfo.isEntirely
leftInfo.height == rightInfo.height
*/
if(leftInfo.isFull && leftInfo.height == rightInfo.height){
isEntirely = true;
}
}
}
int height = Math.max(leftInfo.height,rightInfo.height) + 1;
return new Info(isEntirely,isFull,height);
}