public boolean isComplete() {
if (root == null) return false;
Queue<Node<E>> queue = new LinkedList<>();
queue.offer(root);
boolean leaf = false;
while (!queue.isEmpty()) {
Node<E> node = queue.poll();
if (leaf && !node.isLeaf()) return false;
if (node.left != null && node.right != null) {
queue.offer(node.left);
queue.offer(node.right);
} else if (node.left == null && node.right != null) {
return false;
} else { // 后面遍历的节点都必须是叶子节点
leaf = true;
if (node.left != null) {
queue.offer(node.left);
}
}
}
return true;
}
public boolean isLeaf() {
return left == null && right == null;
}
01-17
2775
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
01-01
09-13
605
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)