分几个情况:1.如果遇到有右子树没有左子树直接返回false
2.如果没有右子树,或者都没有,开启一个新的状态,判断他后面所有的节点是否是叶子节点,此程序包括了测试
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
import javax.swing.tree.TreeNode;
public class test {
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}
public static boolean isCBT(Node head) {
if (head == null) {
return true;
}
Queue<Node> queue = new LinkedList<Node>();
boolean leaf = false;
Node l = null;
Node r = null;
queue.offer(head);
while (!queue.isEmpty()) {
head = queue.poll();
l = head.left;
r = head.right;
if ((leaf && (l != null || r != null)) || (l == null && r != null)) {
return false;
}
if (l != null) {
queue.offer(l);
}
if (r != null) {
queue.offer(r);
} else {
leaf = true;
}
}
return true;
}
// for test -- print tree
public static void printTree(Node head) {
System.out.println("Binary Tree:");
printInOrder(head, 0, "H", 17);
System.out.println();
}
public static void printInOrder(Node head, int height, String to, int len) {
if (head == null) {
return;
}
printInOrder(head.right, height + 1, "v", len);
String val = to + head.value + to;
int lenM = val.length();
int lenL = (len - lenM) / 2;
int lenR = len - lenM - lenL;
val = getSpace(lenL) + val + getSpace(lenR);
System.out.println(getSpace(height * len) + val);
printInOrder(head.left, height + 1, "^", len);
}
public static boolean iswanquan(Node head) {
Queue<Node> qu=new LinkedList();
qu.add(head);
boolean flag=false;
Node l=null;
Node r=null;
while(!qu.isEmpty()) {
head=qu.poll();
l=head.left;
r=head.right;
if(l==null&&r!=null) {
return false;
}
if(flag&&!(l==null&&r==null)) {
return false;
}
if(l!=null) {
qu.add(l);
}
if(r!=null) {
qu.add(r);
}
if(r==null) {
flag=true;
}
}
return true;
}
public static String getSpace(int num) {
String space = " ";
StringBuffer buf = new StringBuffer("");
for (int i = 0; i < num; i++) {
buf.append(space);
}
return buf.toString();
}
public static void main(String[] args) {
Node head = new Node(4);
head.left = new Node(2);
head.right = new Node(6);
head.left.left = new Node(1);
head.left.right = new Node(3);
head.right.right = new Node(5);
printTree(head);
System.out.println(iswanquan(head));
}
}