//判断是否为完全二叉树
public static boolean isCompleteTree(TreeNode root) {
// 层序遍历,直到遇到 null
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (true) {
TreeNode front = queue.poll();
if (front == null) {
break;
}
queue.add(front.left);
queue.add(front.right);
}
// 判断队列中剩余元素是否都是 null
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
if (node != null) {
return false;
}
}
return true;
}