Tree

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
描述
Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes. 
This is an example of one of her creations:

                                                D/ \/   \B     E/ \     \/   \     \A     C     G//F

To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG. 
She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).

Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree. 
However, doing the reconstruction by hand, soon turned out to be tedious. 
So now she asks you to write a program that does the job for her!

输入
The input will contain one or more test cases. 
Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.) 
Input is terminated by end of file.
输出
For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root).
样例输入
DBACEGF ABCDEFG
BCAD CBAD
样例输出
ACBFGED
CDAB
// 输入前缀和中缀写出后缀式, 两种方法//利用递归通过依次遍历前序来组织中序
#include <stdio.h>
#include <string.h>
#include <stdlib.h>#define MAXLEN 27void rebuid_binary_tree(char *pre_str, char *in_str, int len)
{char ch;if(len == 0)                                            //该递归的结束条件为该元素的左右两边都没有元素可以继续return ;ch = *pre_str;int i;for(i = 0; i < len; i++){if(in_str[i] == *pre_str)break;}                                               rebuid_binary_tree(pre_str + 1, in_str, i);                               //左子树rebuid_binary_tree(pre_str + i + 1, in_str + i +1, len - i - 1);       //右子树printf("%c", ch);
}int main()
{char pre_str[MAXLEN], in_str[MAXLEN];while(scanf("%s %s", pre_str, in_str) != EOF){int len = strlen(pre_str);rebuid_binary_tree(pre_str, in_str, len);printf("\n");}return 0;
} // 将字符串分割为三部分
#include<stdio.h>
#include<string.h>char pre_str[27], in_str[27];                       //两个字符串设为全局变量void rcreat(int p_s, int p_e, int i_s, int i_e);    //重建一棵树int main()
{int len;while(scanf("%s%s", pre_str, in_str) != EOF){len = strlen(pre_str) - 1;rcreat(0, len, 0, len); printf("\n");}return 0;
}void rcreat(int p_s, int p_e, int i_s, int i_e)
{if(p_e < p_s)                  // 没有一边的子树的时return;                                                  //两个if都是递归结束的条件if(p_s == p_e)                 //子树只有一个元素时结束{printf("%c", in_str[i_s]);                   //中序和后序都是左子树开始遍历return;}int i;for(i = i_s; ;i++){if(pre_str[p_s] == in_str[i])break;}int len = i - i_s;rcreat(p_s + 1, p_s +len, i_s, i - 1);         //     把相应的字符串传进来rcreat(p_s + len + 1, p_e, i + 1, i_e);printf("%c", in_str[i]);
}

tree(nyoj)相关推荐

  1. Device Tree(三):代码分析【转】

    转自:http://www.wowotech.net/linux_kenrel/dt-code-analysis.html Device Tree(三):代码分析 作者:linuxer 发布于:201 ...

  2. 模板:Link Cut Tree(LCT)

    文章目录 前言 解析 原理 rotate(x) splay(x) access(x) findroot(x) makeroot(x) split(x,y) link(x,y) cut(x,y) pus ...

  3. 纯CSS打造的Family tree(族谱)

    Family tree(族谱),也称家谱,用来记录家族世系繁衍辈份关系.本文结合实例,不借助任何js脚本,使用纯CSS打造一个漂亮的Family tree(族谱),也可以应用的企业组织架构图中. 查看 ...

  4. 竞赛最好用的平衡树-Size Balanced Tree(SBT)【建议收藏】

    大家好. 前段时间我更新过AVL.红黑树以及搜索二叉树的简单讲解.今天我们还是围绕着平衡树的话题,来讲解一个很牛逼的平衡树结构.这种结构是我国的一位大牛:陈启峰.在2006年,他还在读高中阶段,所发明 ...

  5. easyUI前端框架的tree(树)前台展示(树形菜单二)——java

    转载请标明出处:https://blog.csdn.net/men_ma/article/details/106847165. 本文出自 不怕报错 就怕不报错的小猿猿 的博客 easyUI前端框架的t ...

  6. 声明一个Tree(树)类,有成员ages(树龄),成员函数grow(int years)用以对ages 加上years,showage( )用以显示tree对象的ages值。在主函数中定义Tree类对

    //声明一个Tree(树)类,有成员ages(树龄), //成员函数grow(int years)用以对ages 加上years //,showage()用以显示tree对象的ages值. //在主函 ...

  7. 类的练习:定义一个Tree(树)类,有成员ages(树龄),成员函数grow(int years)对ages加上years,age()显示tree对象的ages的值。

    题目 定义一个Tree(树)类,有成员ages(树龄),成员函数grow(int years)对ages加上years,age()显示tree对象的ages的值. 代码 #include<ios ...

  8. 宇宙中的大数:葛立恒数、TREE(3)、SCG3、SSCG3、Rayo数、Fish number 7、BigFoot、Little Bigeddon、Sasquatch (Big Bigeddon)等

    葛立恒数太大了需要用高德纳箭头表示. (一).高德纳箭头的定义为: (二).葛立恒数的定义为: 直观解释葛立恒数: 宇宙的大小为920亿光年=8*10^26米 宇宙最短长度为普朗克长度=1.6*10- ...

  9. Device Tree(三):代码分析

    2019独角兽企业重金招聘Python工程师标准>>> 一.前言 Device Tree总共有三篇,分别是: 1.为何要引入Device Tree,这个机制是用来解决什么问题的?(请 ...

最新文章

  1. 图像处理和计算机视觉中的经典论文
  2. NYOJ 56 阶乘因式分解(一)
  3. pythonclass全局变量_python怎么使用全局变量
  4. 图神经网络(二)GCN的性质(3)GCN是一个低通滤波器
  5. 笔记本中美化代码的方法
  6. php编写一个计算相对路径的函数,php求相对路径的函数
  7. C++ 判断字符串是否全是数字
  8. 计算机技能测试题12答案,计算机基本技能考试选择题及答_计算机一级考试练习题及答案...
  9. python随机抽号_Python基础:手把手以实例教你学随机数产生和字符/ASCII码转换
  10. 23根火柴游戏程序,c语言实现人和计算机一起玩。 每日一题--20200423--
  11. 大牛直播SDK-Windows推送端使用说明
  12. java毕业设计户籍管理系统mybatis+源码+调试部署+系统+数据库+lw
  13. STM8 捕获模式HS0038 红外遥控解码
  14. How to change the implementation (detour) of an externally declared function
  15. R语言计算回归模型R方(R-Squared)实战
  16. STM32上电启动代码详解(转自安富莱电子)
  17. 超强!Figma汉化版下载教程来了!
  18. BatchNormalization 介绍
  19. 调用“抱抱脸团队打造的Transformers pipeline API” 通过预训练模型,快速训练和微调自己的模型
  20. 移动安全app渗透测试之渗透流程、方案及测试要点讲解

热门文章

  1. 使用集成SOA网关的PL / SQL中的REST
  2. 51Nod 1445 变色DNA
  3. js实现网页图片上传本地预览
  4. 利用aspnetPager控件加存储过程实现对gridview的分页
  5. cmd中无法连接MySQL
  6. springcloud的中文文档地址和中国社区
  7. fireFox模拟 post请求、上传插件,火狐浏览器中文postman插件
  8. 性能测试之JMeter配置元件【计数器】
  9. python 列表推导_Python 列表推导式使用的注意事项
  10. kafka java_Java操作Kafka