码迷,mamicode.com
首页 > 其他好文 > 详细

List Leaves

时间:2018-04-04 20:45:01      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:stdio.h   list   leave   between   else   code   from   lines   char   

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

AC代码

 1 #include<stdio.h>
 2 
 3 struct TreeNode{
 4   int Left;
 5   int Right;
 6 }T[10];
 7 
 8 int BTRead(struct TreeNode T[],int N);
 9 void FindLeavesofList(int R, struct TreeNode T[],int N);
10 
11 int main(){
12   int R;
13   int N;
14   scanf("%d\n",&N);
15   R = BTRead(T,N);
16   //printf("%d",R);
17   FindLeavesofList(R,T,N);
18   return 0;
19 }
20 
21 int BTRead(struct TreeNode T[],int N){
22   int Root = 0;
23   //printf("ll%dll",Root);
24   char c1,c2;
25   if(!N){
26     return -1;
27   }
28   int check[N];
29   for(int i = 0; i < N; i++){
30     check[i] = 0;
31   }
32   for(int i = 0; i < N; i++){
33     scanf("%c %c\n",&c1,&c2);
34     if(c1 != -){
35       T[i].Left = c1 - 0;
36       check[T[i].Left] = 1;
37     }
38     else{
39       T[i].Left = -1;
40     }
41     if(c2 != -){
42       T[i].Right = c2 - 0;
43       check[T[i].Right] = 1;
44     }
45     else{
46       T[i].Right = -1;
47     }
48   }
49   //printf("%d",check[0]);
50   int i ;
51   for(i = 0; i < N; i++){
52     if(!check[i]){
53       break;
54     }
55   }
56   Root = i;
57   return Root;
58 }
59 
60 void FindLeavesofList(int R, struct TreeNode T[],int N){
61   int Queue[1000];
62   int pre = 0,rear = 0;
63   int t;
64   int sign = 0;
65   for(int i = 0; i < N; i++){
66     Queue[i] = -1;
67   }
68   if(R == -1){
69     return;
70   }else{
71     Queue[rear] = R;
72     rear++;
73   }
74   while(pre != rear){
75     t = Queue[pre];
76     Queue[pre] = -1;
77     pre++;
78     if(T[t].Left==-1&&T[t].Right==-1&&sign == 0){
79       printf("%d",t);
80       sign = 1;
81   }else if(T[t].Left==-1&&T[t].Right==-1){
82     printf(" %d",t);
83   }
84     if(T[t].Left != -1){
85       Queue[rear] = T[t].Left;
86       rear++;
87     }
88     if(T[t].Right != -1){
89       Queue[rear] = T[t].Right;
90       rear++;
91     }
92   }
93 }

技术分享图片

 



List Leaves

标签:stdio.h   list   leave   between   else   code   from   lines   char   

原文地址:https://www.cnblogs.com/jinjin-2018/p/8718586.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!