Tju_Oj_3988Password
这个题是给树的前序和中序,输出后序。
做法是根据前序找根,根据根在中序中找中序的左右子树,根据左右子树长度找前序的左右子树,依此递归。
做过之后感觉还是比较基础的,废话不多说,上题上代码。
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的更多相关文章
随机推荐
- python-两个图片相似度算法
# -*- coding: UTF-8 -*- """ 作者:zxj 版本:1.0 日期:19-3-24 """ import cv2 im ...
- PAT甲题题解-1121. Damn Single (25)-水题
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789787.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- Ubuntu环境如何上传项目到GitHub网站?
http://blog.csdn.net/ajianyingxiaoqinghan/article/details/70544159
- eclipse实现热部署和热启动
不用每次修改一个class文件就要重启tomcat这么麻烦: http://blog.csdn.net/fuzhongyu2/article/details/52073050
- LeetCode 633. 平方数之和
题目: 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c. 示例1: 输入: 5 输出: True 解释: 1 * 1 + 2 * 2 = 5 示例2 ...
- 电梯V1.0
电梯V1.0 GitHub仓库地址 Problem 一栋3层的大楼(楼层编号0-2),设有一台无限载重的电梯,初始时电梯停在0层.电梯移动1层的耗时为1,在某一层停靠的耗时为1(时间初始为0).电梯不 ...
- beta1
组长:吴晓晖 过去两天完成了哪些任务: 代码重构进行中,界面,预计两个beta单位完成 展示GitHub当日代码/文档签入记录 接下来的计划 更加人性化的推荐算法 还剩下哪些任务 有哪些困难 有哪些收 ...
- springboot项目的创建
创建springboot项目 包名和项目名 选择需要使用的框架,web 然后再点击下一步,完成即可创建springboot项目
- Vue---父子组件之间的通信
在vue组件通信中其中最常见通信方式就是父子组件之中的通信,而父子组件的设定方式在不同情况下又各有不同.最常见的就是父组件为控制组件子组件为视图组件.父组件传递数据给子组件使用,遇到业务逻辑操作时子组 ...
- Go匿名函数
1.GO语言的匿名函数就是闭包 基本概念 闭包是可以包含自由(未绑定到特定对象)变量的代码块,这些变量不在这个代码块内或者任何全局上下文中定义,而是在定义代码块的环境中定义.要执行的代码块(由于自由变 ...