03-树2 List Leaves (25 分)
debug好久
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define null -1
typedef struct Node{
int left;
int right;
}*BitTree;
typedef struct queue{
int n[100];
int rear;
int front;
}Queue;
BitTree input(int n);
void print(BitTree TNode,int n);
int findroot(BitTree TNode,int n);
int find(BitTree TNode,int x,int n);
int main(){
int n;
//scanf("%d\n",&n);
scanf("%d",&n);
BitTree TNode=input(n);
print(TNode,n);
}
BitTree input(int n){
//int n;
BitTree TNode=(BitTree)malloc(sizeof(struct Node)*n*2);
int i=0;
char L,R;
for(i=0;i<n;i++){
scanf("\n%c %c",&L,&R);
//printf(" %c and %c\n",L,R);
if(L=='-'&&R=='-'){TNode[i].left=-1;TNode[i].right=-1;}
if (L!='-'&&R=='-'){TNode[i].left=L-'0';TNode[i].right=-1;}
if(L=='-'&&R!='-'){TNode[i].left=-1;TNode[i].right=R-'0';}
if(L!='-'&&R!='-'){TNode[i].left=L-'0';TNode[i].right=R-'0';}
}//(L!='-') (R!='-')
//for(i=0;i<n;i++){
//printf(" 输出测试%d %d\n",TNode[i].left,TNode[i].right);
//}
return TNode;
}
void print(BitTree TNode,int n){
Queue t;
int con=0;
//int tmp[100];int i=0;
int root=findroot( TNode, n);
//printf("%d\n ",root);
t.n[0]=root;
t.front=0;
t.rear=0;
while(t.front<=t.rear){
if(TNode[root].left!= -1){
t.rear=t.rear+1;
t.n[t.rear]=TNode[root].left;
}
if(TNode[root].right!= -1){
t.rear=t.rear+1;
t.n[t.rear]=TNode[root].right;
}
t.front=t.front+1;
//printf(" root%d\n",root);
//printf(" 删除的队列头元素%d\n",t.n[t.front]);
//printf(" 队列头指针%d\n",t.front) ;
if(TNode[root].left==-1&&TNode[root].right==-1){
if(con!=0)printf(" ");
con=1;
printf("%d",root);
}
root=t.n[t.front];
}
}
/*int findroot(BitTree TNode,int n){
int i=5;int temp;
printf("work1");
for(i=0;i<n;i++){
printf("work2");
int j=0;
for(j=0;j<n;j++){
printf("work3");
if(TNode[j].left==i||TNode[j].right==i) {
temp=1;break;
}
temp=0;
}
if(temp==0){
return i;
}
}
return i;
}*/
int findroot(BitTree TNode,int n){
int a[n-1];
int i=0;
// printf("idezhishi%d",i);
for(i=0;i<n;i++){
// printf("wofule");
a[i]=i;
}
for(i=0;i<n-1;i++){
if(TNode[i].left>=0){
a[TNode[i].left]=-1;
}
if(TNode[i].right>=0){
a[TNode[i].right]=-1;
}
}
for(i=0;i<n;i++){//printf("输出位置%d",a[i]);
if(a[i]>=0){
//printf("%d",a[i]);
return a[i];
}
}
}
```c
在这里插入代码片