Program to determine whether the Tree represented by a binary linked list is a Complete Binary Tree.
Basic Idea: You can use a queue to traverse the layer from top to bottom and from left to right. When a leaf node or no right child node appears for the first time, all the subsequent nodes should be leaf nodes. At the same time, in the traversal, we use the principle that the full Binary Tree "if a node has no left child, there should be no right child" to judge.
Code:
Int judgecomplete (bitree BT) {int tag = 0; bitree P = BT, Q []; If (P = NULL) {return 1;} queueinit (Q ); queuein (Q, P) while (! Queueempty (q) {P = queueout (Q); If (p-> lchild &&! Tag) {queuein (Q, P-> lchild) ;}else {If (p-> lchild) {return 0 ;}else {tag = 1 ;}} if (p-> rchild &&! Tag) {queuein (Q, P-> rchild) ;}else {If (p-> rchild) {return 0 ;}else {tag = 1 ;}}}}