poj1577 Falling Leaves

题意:原先有一颗未知的树, 每次删去树中叶子节点,下次删除上次删除后形成的新的叶子节点,直到删除根,给出每次删除的字母,要求你求出此树的前序遍历。
关键条件: 根左儿子字母都小于根,有孩子都大于根,
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

struct node {

    char ch;
    int l, r;
}tree[100];

char str[105][35];
int n;

void dfs(int i, char ch){

    if(tree[i].ch > ch) {

        if(tree[i].l == 0){

            tree[i].l = n;
            tree[n++].ch = ch;
        }else {

            dfs(tree[i].l, ch);
        }
    }else {

        if(tree[i].r == 0){

            tree[i].r = n;
            tree[n++].ch = ch;
        }else{

            dfs(tree[i].r, ch);
        }
    }
}

void print(int i) {


    printf("%c", tree[i].ch);
    if(tree[i].l) print(tree[i].l);
     if(tree[i].r) print(tree[i].r);
}

int main()
{

    while(scanf("%s", str[0]) != EOF) {

        if(str[0][0] == '$')
            break;
        int k = 0;
        n = 0;
        memset(tree, 0, sizeof(tree));
        while(str[k][0] >= 'A' && str[k][0] <= 'Z'){

                scanf("%s", str[++k]);
        }
          k--;
            tree[n++].ch = str[k][0];
            for(int i = k-1; i >= 0; i--){

                int len = strlen(str[i]);
                for(int j = 0; j < len; j++){

                    dfs(0, str[i][j]);
                }
            }
            print(0);
            printf("\n");
        }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值