03-树2 List Leaves (25 分)

03-树2 List Leaves (25 分),第1张

概述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 po

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 N1. 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:
81 -- -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 总结

以上是内存溢出为你收集整理的03-树2 List Leaves (25 分)全部内容,希望文章能够帮你解决03-树2 List Leaves (25 分)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/1025418.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-23
下一篇2022-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存