题解 | JAVA #判断是不是完全二叉树# [P0-T2]
判断是不是完全二叉树
http://www.nowcoder.com/practice/8daa4dff9e36409abba2adbe413d6fae
跟这题一样的 https://blog.nowcoder.net/n/4012546b98ae4356ab926184ed323c58
import java.util.*;
public class Solution {
public boolean isCompleteTree (TreeNode root) {
if (root == null) return false;
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
boolean ended = false;
while(!q.isEmpty()) {
TreeNode pop = q.poll();
if (pop == null) {
ended = true;
} else {
if (ended) return false;
q.offer(pop.left);
q.offer(pop.right);
}
}
return true;
}
}