洛谷P1305: 新二叉树

https://www.luogu.org/problemnew/show/P1305

题目描述

输入一串二叉树,用遍历前序打出。

输入输出格式

输入格式:

第一行为二叉树的节点数n。(n≤26)

后面n行,每一个字母为节点,后两个字母分别为其左右儿子。

空节点用*表示

输出格式:

前序排列的二叉树

输入输出样例

输入样例#1: 复制

6
abc
bdi
cj*
d**
i**
j**

输出样例#1: 复制

abdicj

不能用getchar()我也不知道为什么。

#include <stdio.h>
#define N 130
using namespace std;
struct data{
    char l;
    char r;
}str[N];
void tree(char root)
{
    if(root=='*')
    	return ;
    printf("%c", root);
    tree(str[root].l);
    tree(str[root].r);
    return ;
}
int main()
{
    int i, n;
    char root, a, b, c;
    scanf("%d", &n);
    for(i=0; i<n; i++)
    {
        //getchar();
        scanf(" %c", &a);
        scanf("%c%c", &str[a].l, &str[a].r);
        if(i==0)
        	root=a;
    }
    tree(root);
    return 0;
}
原文地址:https://www.cnblogs.com/zyq1758043090/p/11852570.html