# 272. Completeness test of binary tree

Title Description:

Given a binary tree, determine whether it is a complete binary tree.

The definition of complete binary tree in Baidu Encyclopedia is as follows:

If the depth of the binary tree is h, the number of nodes in all layers (1-h-1) except the H layer reaches the maximum, and all nodes in the H layer are continuously concentrated on the left, which is the complete binary tree. (Note: layer h may contain 1-2h nodes.)

Example 1:

Input: [1,2,3,4,5,6]
Output: true
Explanation: each layer before the last layer is full (that is, two layers with node values of {1} and {2,3}), and all nodes in the last layer ({4,5,6}) are left as far as possible.
Example 2: Input: [1,2,3,4,5,null,7]
Output: false
Explanation: nodes with a value of 7 are not as far to the left as possible.

Tips:

There will be 1 to 100 nodes in the tree.

Source: LeetCode
At the beginning, my idea was rather complicated. First, I calculated the height of the binary tree, and then we traversed layer by layer. When we met the penultimate layer, we need to pay attention to it
Redundancy in code

```/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
class Solution {

public boolean isCompleteTree(TreeNode root) {
int dep = getdep(root)  - 1;
queue.offer(root);
int curdep = 0;
while (!queue.isEmpty()){
if(curdep == dep){
return true;
}
int size = queue.size();
if(curdep < dep - 1){
for (int i = 0; i < size; i++) {
TreeNode poll = queue.poll();
if(poll.left == null || poll.right == null){
return false;
}
queue.offer(poll.left);
queue.offer(poll.right);
}
//                curdep ++;
}else{
boolean flag = true;
for (int i = 0; i < size; i++) {
TreeNode poll = queue.poll();
if(poll.right != null && poll.left == null){
return  false;
}
//                    There's a
if((poll.left != null || poll.right != null) && !flag){
return false;
}
if((poll.left != null && poll.right == null) || (poll.left == null && poll.right == null)){
flag = false;
}
if(poll.left != null){
queue.offer(poll.left);
}
if(poll.right != null){
queue.offer(poll.right);
}
}

//                curdep ++;
}

curdep ++;
}
return false;

}
public int getdep(TreeNode root){
if(root == null){
return 0;
}
int left = getdep(root.left);
int right = getdep(root.right);
return Math.max(left,right) + 1;
}
}
```

See what others have achieved, simple and clear
In fact, it's very simple. As long as you traverse to the null node, see if there is any. If there is any, return false. Otherwise, it's true

```class Solution {
/**
* Span traversal binary tree, when there is a null value, stop traversing, if there is no node traversed at this time, it means that the tree is not a complete binary tree.
*/
public boolean isCompleteTree(TreeNode root) {
TreeNode cur;
while ((cur = q.removeFirst()) != null) {
}
while (!q.isEmpty()) {
if (q.removeLast() != null) {
return false;
}
}
return true;
}
}
```  797 original articles published, praised 23, visited 30000+

Tags: network

Posted on Wed, 05 Feb 2020 23:57:31 -0500 by oakld