7-4 List Leaves (25 分)

原题

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

结尾无空行

解题思路:

很久没接触过树啦,但还是写了一下,可思路还算清晰,但写法命名之类应该不太规范!见谅

题目意思容易理解,可以发现输入为“- -”,即没有子结点的叶结点。主要在于输出叶结点的顺序,为从顶到底,从左到右,需要用到队列的思想。

关键在于:

1.找到头结点:可以建立hash表,在输入中没有出现的结点即没有作为子结点,该结点就是头结点

2.叶结点的顺序:采用队列的思想,头结点先入队,然后每次dequeue队列头的结点,将该结点的子结点入队,直到所有结点都入队过,可确定从顶到底、从左到右的结点顺序,然后按该顺序去找叶结点,没有子结点的结点就是叶结点。

如样例中给出的树,长这样。

括号内为进队顺序

3进队(3)

3出 2、7依次进(3-2-7)

2-7

2出0进(3-2-7-0)

7-0

7出4、6依次进(3-2-7-0-4-6)

0-4-6

0出1进(3-2-7-0-4-6-1)

4-6-1

4出5进(3-2-7-0-4-6-1-5)

此时所有结点都已经入队过一次,结束

最终序列确定3-2-7-0-4-6-1-5

确定序列就可以按照按顺序判断4、1、5为叶结点

答案(AC)

#include <stdio.h>
#include <stdlib.h>

typedef struct Node{
    int left;
    int right;
}Node;
typedef struct sequence{
    int ptr;
    int num;
    int *array;
}*ptrs;
void findleaves(int i,Node nodes[]);
ptrs CreateTree (int head,Node nodes[],int N);
int Dequeue(ptrs S){
    return S->array[S->ptr++];
}
void Add(ptrs S,int i){
    S->array[S->num++]=i;
}
int main(){
    int flag=0;
    int N;
    int i,j;
    int head;
    char l,r;
    scanf("%d",&N);
    Node nodes[N];
    int sequence[N];
    int hash[N];
    for(i=0;i<N;i++){
        hash[i]=0;
    }//in order to find the head of tree
    for(i=0;i<N;i++){
        scanf(" %c %c",&l,&r);
        if(l=='-'){
            nodes[i].left=-1;//use -1 to  reprensent the null
        }else{
            nodes[i].left=l-'0';
            hash[nodes[i].left]=1;
        }
        if(r=='-'){
            nodes[i].right=-1;
        }else{
            nodes[i].right=r-'0';
            hash[nodes[i].right]=1;
        }
    }
    for(i=0;i<N;i++){
        if(hash[i]==0){
            head=i;
            break;
        }
    }
    ptrs S=CreateTree(head,nodes,N);
    for(i=0;i<N;i++){
        j=S->array[i];
        if(nodes[j].left==-1 && nodes[j].right==-1){
        if(!flag)flag=1;
        else printf(" ");
        printf("%d",j);
        }
    }
    return 0;
}

ptrs CreateTree (int head,Node nodes[],int N){
    ptrs S=(ptrs)malloc(sizeof(struct sequence));
    int i;
    S->ptr=0;
    S->array=(int*)malloc(sizeof(int)*N);
    S->array[0]=head;
     S->num=1;
    while(S->num<N){
        i=Dequeue(S);
    if(nodes[i].left!=-1){
        Add(S,nodes[i].left);
    }
    if(nodes[i].right!=-1){
        Add(S,nodes[i].right);
    }
    }
    return S;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值