加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

03-树2 List Leaves (25 分)

发布时间:2020-12-14 00:57:00 所属栏目:Linux 来源:网络整理
导读: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?( ≤) which is the total

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?(≤) 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
?

  1 #include <cstdio> 
  2 #include <stdlib.h>
  3 const int maxn = 12;
  4 struct TreeNode {
  5     int lchild,rchild;
  6 } tree[maxn];
  7 
  8 typedef int ElementType;
  9 typedef int Position;
 10 struct QNode {
 11     ElementType *Data;     /* 存储元素的数组 */
 12     Position Front,Rear;  /* 队列的头、尾指针 */
 13     int MaxSize;           /* 队列最大容量 */
 14 };
 15 
 16 typedef struct QNode *Queue;
 17  
 18 Queue CreateQueue( int MaxSize )
 19 {
 20     Queue Q = (Queue)malloc(sizeof(struct QNode));
 21     Q->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
 22     Q->Front = Q->Rear = 0;
 23     Q->MaxSize = MaxSize;
 24     return Q;
 25 }
 26  
 27 bool IsFull( Queue Q )
 28 {
 29     return ((Q->Rear+1)%Q->MaxSize == Q->Front);
 30 }
 31  
 32 bool AddQ( Queue Q,ElementType X )
 33 {
 34     if ( IsFull(Q) ) {
 35         printf("队列满");
 36         return false;
 37     }
 38     else {
 39         Q->Rear = (Q->Rear+1)%Q->MaxSize;
 40         Q->Data[Q->Rear] = X;
 41         return true;
 42     }
 43 }
 44  
 45 bool IsEmpty( Queue Q )
 46 {
 47     return (Q->Front == Q->Rear);
 48 }
 49  
 50 ElementType DeleteQ( Queue Q )
 51 {
 52     if ( IsEmpty(Q) ) { 
 53         printf("队列空");
 54         return -1;
 55     }
 56     else  {
 57         Q->Front =(Q->Front+1)%Q->MaxSize;
 58         return  Q->Data[Q->Front];
 59     }
 60 }
 61 
 62 int buildTree() {
 63     int n;
 64     scanf("%d",&n);
 65 
 66     for(int i=0; i<n; i++) {
 67         getchar();
 68         char tl,tr;
 69         scanf("%c %c",&tl,&tr);
 70         if(tl == -) tree[i].lchild = -1;
 71         else tree[i].lchild = tl - 0;
 72         if(tr == -) tree[i].rchild = -1;
 73         else tree[i].rchild = tr - 0;
 74     }
 75     return n;
 76 }
 77 int num = 0;
 78 void traversal(int root) {
 79     if(root != -1) {
 80         num++;
 81         traversal(tree[root].lchild);
 82         traversal(tree[root].rchild);
 83     }
 84 }
 85 int findroot(int maxsize) {
 86     
 87     for(int i=0; i<maxsize; i++) {
 88         num = 0;
 89         traversal(i);
 90         if(num == maxsize) {
 91             return i;
 92         }
 93     }
 94     
 95 }
 96 
 97 int flag = 1;
 98 void lOTrav(int root) {
 99     Queue Q;
100     int temp;
101     
102     if(root == -1) return;
103     
104     Q = CreateQueue(12);
105     AddQ(Q,root);
106     while(!IsEmpty(Q)) {
107         temp = DeleteQ( Q );
108         if(tree[temp].lchild == -1 && tree[temp].rchild == -1) {
109             if(flag) {
110                 printf("%d",temp);
111                 flag = 0;
112             }
113             else printf(" %d",temp);
114         }
115         if(tree[temp].lchild != -1) AddQ( Q,tree[temp].lchild );
116         if(tree[temp].rchild != -1) AddQ( Q,tree[temp].rchild );
117         
118         
119     }
120     
121 } 
122 
123 
124 int main() {
125     int maxsize = buildTree();
126     int roota = findroot(maxsize);
127     lOTrav(roota);
128     
129     
130     
131     return 0;
132 }
View Code

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读