这个题是给树的前序和中序,输出后序。

做法是根据前序找根,根据根在中序中找中序的左右子树,根据左右子树长度找前序的左右子树,依此递归。

做过之后感觉还是比较基础的,废话不多说,上题上代码。

Bob will get a bag as gift from Alice, but Alice don't wanna Bob get the bag without did anything, so she put the bag into a safe box... Alice will give two hints about password to Bob. One is the preorder traversal(root, left subtree, right subtree) of a binary tree, another is the inorder traversal(left subtree, root, right subtree) of the same tree. The password is the postorder traversal(left subtree, right subtree, root) of the tree.
For example:

The tree only has three nodes A, B and C.
Its preorder traversal is ABC, inorder traversal is BAC, and postorder traversal is BCA.

Input

There are several test cases in the input file, Each test case contains two line. Preorder traversal and Inorder traversal.(Each line's length won't longer than 26, and only contain upper letter)

Output

For each test case, output the password Bob need.

Sample Input

ABC
BAC

Sample Output

BCA
/*
* 3988_Password.cpp
* 给出字母的前序和中序,求后序
* Created on: 2018年11月8日
* Author: Jeason
*/ #include <iostream>
#include <fstream>
#include <string>
#include <vector>
#define MAX 100
using namespace std; void find(string tree_fro , string tree_mid)
{
char root = tree_fro[]; int num_leftSubTree = tree_mid.find(root);
int num_rightSubTree = tree_mid.length() - num_leftSubTree - ;
if ( tree_fro.length() == ){
cout << root ;
return;
}
if(num_leftSubTree != ) {
string leftSubTree_mid = tree_mid.substr( , num_leftSubTree );
string leftSubTree_fro = tree_fro.substr( ,num_leftSubTree);
find( leftSubTree_fro , leftSubTree_mid );
}
if(num_rightSubTree != ) {
string rightSubTree_mid = tree_mid.substr( num_leftSubTree + ,num_rightSubTree );
string rightSubTree_fro = tree_fro.substr(num_leftSubTree + ,num_rightSubTree );
find( rightSubTree_fro , rightSubTree_mid );
} cout << root;
return;
} int main()
{
string tree_fro;
string tree_mid;
while(cin >> tree_fro){
cin >> tree_mid;
find(tree_fro , tree_mid);
cout << endl;
} return ;
} /*
Sample Input ABC
BAC
Sample Output BCA */

Tju_Oj_3988Password的更多相关文章

随机推荐

  1. 如何在一个电脑上同时使用两个Git的账号

    前言 又需要登录公司的账号,又想在电脑上使用自己的账号. 实现 首先是git config方面的设置,要取消掉原本对于git账号的全局设置. git config --global --unset u ...

  2. 第二阶段Sprint8

    昨天:把视频录制整合到时间提醒里,实现视频提醒 今天:重新规划主界面,把视频录制暂放到主页面里,先实现功能,视频提醒后期再做. 遇到的问题:还是有问题,虽然能运行,但是只能播放,不能录了啊...

  3. 单片机内程序运行的时候ram空间是如何分配的?

    转自:http://blog.sina.com.cn/s/blog_a575eb9401014tam.html 单片机内程序运行的时候ram空间是如何分配的?我现对一个程序进行减少片内ram的使用的优 ...

  4. Linux基础六(网络管理)

    目录 一.网络配置 1. IP 地址配置 2. 网络配置文件 3. 虚拟机网络配置参数 二.网络命令 1. 网络环境查看命令 2. 网络测试命令 三.远程会话安全协议-SSH原理 1. SSH 原理 ...

  5. Beta 冲刺 五

    团队成员 051601135 岳冠宇 031602629 刘意晗 031602248 郑智文 031602330 苏芳锃 031602234 王淇 照片 项目进展 岳冠宇 昨天的困难 数据交换比较复杂 ...

  6. 用prop还是attr

    jquery中attr和prop的区别   在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别?这些问题就出现了. 关于它们两个的区别,网上的答 ...

  7. Sublime Text添加插入带当前时间说明

    Sublime Text添加插入带当前时间说明   作者:木尘 日期:2014-11-25 插件实现插入带时间功能的说明: 1. 创建插件: Tools → New Plugin: import da ...

  8. 微信 小程序组件 加入购物车全套 one wxss

    //1,wxss /*外部容器*/ .container { display: flex; flex-direction: column; align-items: center; justify-c ...

  9. UML实践详细经典教程

    面向对象的问题的处理的关键是建模问题.建模可以把在复杂世界的许多重要的细节给抽象出.许多建模工具封装了UML(也就是Unified Modeling Language™),这篇课程的目的是展示出UML ...

  10. 跨语言通信方案的比较—Thrift、Protobuf和Avro

    Thrift由Facebook开源的一个RPC框架,用来进行可扩展且跨语言的服务的开发,使得各种编程语言间无缝结合的.高效的服务.我们依据Thrift的规范 简单定义访问接口,通过Thrift编译器编 ...