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

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

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

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. Nuxeo 认证绕过和RCE漏洞分析(CVE-2018-16341)

    简介 Nuxeo Platform是一款跨平台开源的企业级内容管理系统(CMS).nuxeo-jsf-ui组件处理facelet模板不当,当访问的facelet模板不存在时,相关的文件名会输出到错误页 ...

  2. imagick用法!

    https://coderwall.com/p/9hj97w sudo apt-get install imagemagick sudo apt-get install php5-imagick su ...

  3. PAT甲题题解-1128. N Queens Puzzle (20)-做了一个假的n皇后问题

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789810.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  4. Daily Scrum NO.3

    工作概况 符美潇(PM) 昨日完成的工作 1.Daily Scrum.日常会议及日常工作的分配和查收. 2.整合各DEV所写的代码,在TFS上进行Beta阶段第一次代码签入. 今日工作 1.Daily ...

  5. Daily Scrum NO.2

    工作概况 符美潇(PM) 昨日完成的工作 1.Daily Scrum.日常会议及日常工作的分配和查收. 2.为两名团队新成员制定了任务并录入TFS. 今日工作 1.Daily Scrum.日常会议及日 ...

  6. java实验报告三

    实验三 敏捷开发与XP实践 一.实验内容 1. XP基础 2. XP核心实践 3. 相关工具 二.实验步骤 (一)敏捷开发与XP 软件工程是把系统的.有序的.可量化的方法应用到软件的开发.运营和维护上 ...

  7. 在centOS中安装mongodb

    自己在一个CentOS6.6的系统中按照官网的说明,走了一遍的安装过程,记录一下. 看过个mongo的视频,上面介绍的安装是用源码安装,而官网上说需要gcc4.8.3的版本,还有scons的编译工具, ...

  8. UIO,大页内存,CPU亲和性,NUMA机制等

    Linux环境下的UIO(Userspace I/O) UIO 用户空间下驱动程序的支持机制.DPDK使用UIO机制使网卡驱动程序运行在用户态,并采用轮询和零拷贝方式从网卡收取报文,提高收发报文的性能 ...

  9. 作业四 任务分解(WBS)

    近日忙于实验,未来得及完成任务分解昨晚召开了紧急会议,才确定了任务划分.主体分配如下:三名编程人员,一个主编两个辅编,一人做需求分析,一人做程序测试,一人专司文档. 具体细节如下:在剩余的三周左右的时 ...

  10. 单片机pc指针

    单片机的PC是指程序计数器(Program Counter).程序计数器PC用于存放下一条将要执行的指令地址,是一个16位专用寄存器,不能通过MOV指令来操作,对用户来说是不可见的.当执行一条指令时, ...