P1305 新二叉树

题目描述

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

输入格式

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

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

空节点用 * 表示

输出格式

二叉树的前序遍历。

输入输出样例

输入 #1

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

输出 #1

abdicj

/*
 * @Description: To iterate is human, to recurse divine.
 * @Autor: Recursion
 * @Date: 2022-03-10 16:21:12
 * @LastEditTime: 2022-03-10 16:31:26
 */
#include<bits/stdc++.h>
using namespace std;

int n;

struct node{
    char lc,rc;//左右孩子和父节点
};
node tree[10001];

void dfs(char x)
{
    if(x=='*') return;
    cout<<x;
    dfs(tree[x].lc);
    dfs(tree[x].rc);
}

int main()
{
    cin>>n;
    char h1;
    cin>>h1;//输入第一个字母
    cin>>tree[h1].lc>>tree[h1].rc;//h1所代表的字符再次会转换为ASC码 
    for(int i=2;i<=n;i++){
        char h;
        cin>>h;
        cin>>tree[h].lc>>tree[h].rc;
    }
    dfs(h1);
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值