import java.util.LinkedList;
import java.util.Queue;
public class CBTree {
public static void main(String[] args) {
Node root = new Node(1);
Node node2 = new Node(2);
Node node3 = new Node(3);
Node node4 = new Node(4);
Node node5 = new Node(5);
Node node6 = new Node(6);
Node node7 = new Node(7);
Node nodeEx = new Node(0);
root.left = node2;
root.right = node3;
node2.left = node4;
node2.right = node5;
node3.left = node6;
node3.right = node7;
node6.left = nodeEx;
Queue<Node> queue = new LinkedList<Node>();
boolean res = isCBT(root, queue);
System.out.println(res);
}
static boolean isLeaf = false;
static boolean isOneTree = false;
static boolean isCBT(Node root, Queue<Node> queue) {
queue.add(root);
while (!queue.isEmpty()) {
Node cur = queue.poll();
if ((isLeaf && (cur.left != null || cur.right != null))
|| (cur.right != null && cur.left == null)
) {
return false;
}
if (/*左右孩子都没有*/ cur.right == null || cur.left == null) {
//指示器变TRUE
isLeaf = true;
}
if (cur.left != null) queue.add(cur.left);
if (cur.right != null) queue.add(cur.right);
}
return true;
}
}
判断完全二叉树 isCBT Complete Binary Tree
最新推荐文章于 2024-09-01 15:30:33 发布