題目:輸入一顆二叉樹,從上往下按層打印樹的每個節點,同一層中按照從左往右的順序打印。代碼如下:
#define MAXLEN 100
void leveltraverse (tree *head) {
tree *temp = head;
tree *queue[MAXLEN];
int rear, front;
rear = front = 0;
if (NULL == head)
return;
queue[rear++] = head;
while (rear != front) {
printf("%d", queue[front]->element);
if (queue[front] -> left != NULL)
//insert
if (queue[front] -> right != NULL)
//insert
front = (front + 1) % MAXLEN;
}
return;
}