当前位置:网站首页>7-1 前序序列创建二叉树
7-1 前序序列创建二叉树
2022-06-22 20:24:00 【白—】
7-1 前序序列创建二叉树
编一个程序,读入用户输入的一串先序遍历字符串,根据此字符串建立一个二叉树(以二叉链表存储)。 例如如下的先序遍历字符串: ABC##DE#G##F### 其中“#”表示的是空格,代表一棵空树。然后再对二叉树进行中序遍历,输出遍历结果。
输入格式:
多组测试数据,每组测试数据一行,该行只有一个字符串,长度不超过100。
输出格式:
对于每组数据,
输出二叉树的中序遍历的序列,每个字符后面都有一个空格。
每组输出一行,对应输入的一行字符串。
输入样例:(及其对应的二叉树)
abc##de#g##f###
输出样例:
c b e g d f a
代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct node *BinTree;
char str[110];
int i;
struct node
{
char data;
BinTree left;
BinTree right;
};
BinTree CreatTree(BinTree T)
{
char x;
x=str[i++];
if(x=='#')
T=NULL;
else
{
BinTree newnode=(BinTree)malloc(sizeof(struct node));
newnode->left=newnode->right=NULL;
newnode->data=x;
T=newnode;
T->left=CreatTree(T->left);
T->right=CreatTree(T->right);
}
return T;
}
void print(BinTree T)
{
if(T==NULL)
return;
else
{
print(T->left);
printf("%c ",T->data);
print(T->right);
}
}
int main()
{
while(scanf("%s",str)!=EOF)
{
BinTree T=CreatTree(T);
print(T);
i=0;
printf("\n");
}
return 0;
}
202206201631一
边栏推荐
- 基于C语言开发工资管理系统 课程论文+源码及可执行exe文件
- 解决phpstudy中mysql无法启动,与本地安装的mysql冲突
- Leetcode daily question - 513 Find the value in the lower left corner of the tree
- Jerry's plug-in 4m flash to view the processing method with a size of only 1m on the PC [chapter]
- 数据科学家是不是特有前途的职业?
- Campus errand management app Shaanxi Gechuang
- Adblock屏蔽百度热搜
- 鸿蒙第三次培训
- [redis] publish and subscribe
- 微软 Edge 浏览器将支持网络测速,内置计算器和单位转换工具
猜你喜欢

IDC releases China Data Governance Report Yixin Huachen No. 1

引入稀疏激活机制!Uni-Perceiver-MoE显著提升通才模型的性能

HarmonyOS应用开发培训第二次

Redis的使用场景分享(项目实战)

为了不曾忘却的纪念:孙剑专题

什么是数据资产?数据资产管理应该如何落地?

微软 Edge 浏览器将支持网络测速,内置计算器和单位转换工具

Lesson 018: function: flexible is powerful after class test questions and answers

Lesson 030: file system: introduce a big thing | after class test questions and answers

杰理之外挂 4M 的 flash 在 PC 上查看大小只有 1M 的处理方法【篇】
随机推荐
微软 Edge 浏览器将支持网络测速,内置计算器和单位转换工具
为了不曾忘却的纪念:孙剑专题
Oracle数据库中文字符串和英文字符串的截取不同
Lesson 028: Documents: because I know you, I will never forget the after-school test questions and answers [no title]
[redis]redis6 master-slave replication
Lesson 022: function: recursion is god horse after class test questions and answers
IDC發布中國數據治理報告 億信華辰第一
72 results and development suggestions of the latest on-site production system optimization
Redis core technology and practice: learning summary directory
优化求解器 | Gurobi的MVar类:矩阵建模利器、求解对偶问题的备选方案 (附详细案例+代码)
300. longest increasing subsequence ●●
Kali2021 installing the rtl8188gu wireless network card [tl-wn726n] driver
[redis] cluster and common errors
Lesson 023 and 024: recursion: these little bunnies, Hanoi Tower after class test questions and answers
杰理之开启四声道通话近端卡顿问题【篇】
88- widely circulated parameter optimization, honey or poison?
TC397 Flash
杰理之AUX 模式使用 AUX1或者 AUX2通道时,程序会复位问题【篇】
杰理之硬件上 DACL 输出,DAC 输出左右声道的声音【篇】
长安旗下阿维塔科技增资扩股落定:宁德时代将持股约24%!