Untitled
unknown
plain_text
10 months ago
855 B
5
Indexable
public class Solution {
public boolean isCompleteTree(TreeNode root) {
if (root == null) return true;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
boolean foundNull = false;
while (!queue.isEmpty()) {
TreeNode current = queue.poll();
if (current == null) {
foundNull = true; // Mark the point where null is encountered
} else {
if (foundNull) {
// If we've already encountered a null and now see a non-null node, it's not complete
return false;
}
queue.offer(current.left);
queue.offer(current.right);
}
}
return true; // All nodes satisfied the completeness condition
}
}
Editor is loading...
Leave a Comment