#C4318. Complete Binary Tree Checker
Complete Binary Tree Checker
Complete Binary Tree Checker
This problem requires you to determine whether a given binary tree is complete. A binary tree is said to be complete if every level, except possibly the last, is completely filled and all nodes in the last level are as far left as possible. More formally, if we number the nodes level-wise starting at 1, then a binary tree is complete if for every node with index i, the left child is at index 2i and the right child is at index 2i+1 (if they exist), and there are no gaps in the sequence.
You will be given a level order traversal of the binary tree. Each value is either an integer (indicating a node’s value) or the string null
indicating that there is no node at that position. Your task is to build the tree from this input and then check if the tree is complete.
inputFormat
The input consists of a single line that contains space-separated tokens representing the level order traversal of a binary tree. Each token is either an integer or the string null
(which represents a missing node). For example:
1 2 3 4 5 6
represents a complete binary tree, while
1 2 3 4 5 null 7
represents an incomplete binary tree.
outputFormat
Output a single line containing True
if the binary tree is complete, or False
otherwise.
1 2 3 4 5 6
True