Check if a given binary tree is completed. A complete binary tree is one in which every level of the binary tree is completely filled except possibly the last level. Furthermore, all nodes are as far left as possible.
Examples
5
/ \
3 8
/ \
1 4
is completed.
5
/ \
3 8
/ \ \
1 4 11
is not completed.
Corner Cases
How is the binary tree represented?
We use the level order traversal sequence with a special symbol "#" denoting the null node.
For Example:
The sequence [1, 2, 3, #, #, 4] represents the following binary tree:
1
/ \
2 3
/
4
思路使用队列按层次遍历的顺序进行,使用flag记录上一层的状态,默认为false 即对应第一个遍历到根节点的状态。
flag 与 节点状态对应如下:
节点为空 flag = true
节点非空 flag = false
运行思路
若该树是完整的,则只有遍历到最后