03-树2 List Leaves

本文介绍了一种算法,用于按从上到下、从左到右的顺序列出给定树的所有叶子节点。通过建立树结构并使用队列进行层序遍历来实现,最后输出所有叶子节点的索引。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:

8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6

Sample Output:

4 1 5

 

 1 #include<stdio.h>
 2 #include<stdlib.h> 
 3 #define max 10
 4 #define Null -1
 5 
 6 struct TNode{
 7     int data;
 8     int left, right;
 9 }T[max];
10 
11 struct QNode{
12     int *data;
13     int front, rear;
14 };
15 typedef struct QNode *Queue;
16 
17 //建树函数,返回根结点Root 
18 int BuildTree( ){
19     int N , Root= Null, check[max]={0};
20     char L, R;
21     scanf("%d", &N);
22     for(int i=0; i<N; i++){
23         scanf("\n%c %c", &L, &R);
24         T[i].data = i;
25         if(L=='-'){
26             T[i].left = Null;
27         }
28         else {
29             T[i].left = L-'0';
30             check[T[i].left] = 1;
31         }
32         if(R=='-'){
33             T[i].right = Null;
34         }
35         else {
36             T[i].right = R-'0';
37             check[T[i].right] = 1;
38         }
39     }
40     for(int i=0; i<N; i++){
41         if(!check[i]){
42             Root = i;
43             break;
44         }
45     }
46     return Root;
47 }
48 
49 //初始化队列函数,返回队列表头 
50 Queue CreateQueue(Queue Q){
51     Q = (Queue)malloc(sizeof(struct QNode));
52     Q->data = (int*)malloc(max*sizeof(int));
53     Q->front = Q->rear = 0;
54     return Q;
55 }
56 
57 //进队列函数,因为知道数据最大个数,一定不会溢出,所以不需要判断是否队满 
58 void Add(Queue Q, int n) {
59     Q->data[Q->front++] = n;
60 }
61 
62 bool IsEmpty(Queue Q){
63     return(Q->front==Q->rear);
64 }
65 
66 //出队列函数 
67 int Delete(Queue Q){
68     int temp = Q->data[Q->rear++];
69     return temp;
70 }
71 
72 void LevelorderTraversal(int Root){
73     if(Root==Null) return;            //如果树为空直接返回 
74     int flag = 0;        //flag用于调整输出格式 
75     Queue Q = CreateQueue(Q);         
76     Add(Q, Root);       
77     while(!IsEmpty(Q)){            //只要队列不空 
78         int temp = Delete(Q);      //将队尾元素出队 
79         if(T[temp].left==Null && T[temp].right==Null){    //如果该元素的左右子树均为空,即leaves 
80             if(!flag) flag=1;
81             else printf(" ");
82             printf("%d", temp);                           //打印该元素 
83         }
84         if(T[temp].left!=Null) Add(Q, T[temp].left);      //如果该元素左子树不空,将左子树的根结点进队 
85         if(T[temp].right!=Null) Add(Q, T[temp].right);    //同理右子树  因为题意要从左到右,所以先判断左子树在判断右子树 
86     }
87 }
88 
89 //主程序框架 
90 int main( ){
91     int Root;
92     Root = BuildTree( );
93     LevelorderTraversal(Root);
94     return 0;
95 }

 

转载于:https://www.cnblogs.com/shin0324/p/9801636.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值