Untitled

 avatar
unknown
plain_text
a month ago
855 B
2
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
    }
}
Leave a Comment