/*List Leaves*/
/*思路:遍历树找出叶节点,然后对树进行层序遍历,按层序遍历顺序输出叶节点即可*/
#include<stdio.h>
#include<stdlib.h>
typedef struct tree {
int left;
int right;
}Tree;
/*队列数组*/
typedef struct Qnode* ptrtoQnode;
struct Qnode {
int* data;
int front, rear;/*队列头尾指针*/
int maxsize;/*容量*/
};
typedef ptrtoQnode Queue;
/*以下几个函数为队列操作集*/
Queue CreateQueue(int maxsize);
int Isfull(Queue Q);
void AddQ(Queue Q, int x);
int Isempty(Queue Q);
int DeleteQ(Queue Q);
int main() {
int N;/*节点数*/
char laji;/*用来除去\n*/
scanf("%d", &N);
Tree* tree = (struct tree*)malloc(N * sizeof(struct tree));
for (int i = 0; i < N; i++) {
scanf("%c",&laji);
if (!scanf("%d", &tree[i].left)) tree[i].left = -1; /*空子节点为-1*/
if (!scanf("%d", &tree[i].right)) tree[i].right = -1;
}/*输入完成*/
/*测试输出*/
/*
for(int i =0;i<N;i++){
printf("%d %d\n",tree[i].left,tree[i].right);
}
*/
/*找根节点*/
int root;
int* array = (int*)malloc(N * sizeof(int));/*标记数组,辅助作用*/
for (int i = 0; i < N; i++) array[i] = 1;/*全部设为1*/
for (int i = 0; i < N; i++) { /*不是根节点设为0*/
if (tree[i].left!=-1) array[tree[i].left] = 0;
if (tree[i].right!=-1) array[tree[i].right] = 0;
}
for (int i = 0; i < N; i++) {
if (array[i]) {
root = i;
break;
}
}/*寻找完毕*/
/*找叶节点*/
int numberofleaves=0;
for (int i = 0; i < N; i++) {
if (tree[i].left == -1 && tree[i].right == -1){
array[i] = -1; /*叶节点标记为-1*/
numberofleaves++;
}
}/*over*/
/*测试array数组*/
/*
for(int i = 0;i<N;i++) {
printf("%d\n",array[i]);
}
*/
/*层序遍历,并输出*/
Queue Q;
int k;
Q = CreateQueue(11);
AddQ(Q, root);
while (!Isempty(Q)) {
k = DeleteQ(Q);
if (array[k] == -1) {
printf("%d", k);
numberofleaves--;
if (numberofleaves != 0) printf(" ");
}
if (tree[k].left != -1) AddQ(Q, tree[k].left);
if (tree[k].right != -1) AddQ(Q, tree[k].right);
}
return 0;
}
/*队列*/
Queue CreateQueue(int maxsize) {
Queue Q = (Queue)malloc(sizeof(struct Qnode));
Q->data = (int*)malloc(maxsize * sizeof(int));
Q->front = Q->rear = 0;
Q->maxsize = maxsize;
return Q;
}
int Isfull(Queue Q) {
return((Q->rear + 1) % Q->maxsize == Q->front);
}
void AddQ(Queue Q, int x) {
Q->rear = (Q->rear + 1) % Q->maxsize;
Q->data[Q->rear] = x;
}
int Isempty(Queue Q) {
return(Q->rear == Q->front);
}
int DeleteQ(Queue Q) {
Q->front = (Q->front + 1) % Q->maxsize;
return Q->data[Q->front];
}
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N−1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a "-" will be put at the position. Any pair of children are separated by a space.
Output Specification:
For each test case, print in one line all the leaves' indices in the order of top down, and left to right. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.
Sample Input:
8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6
Sample Output:
4 1 5