POJ 2255 Tree Recovery

poj_2255_Tree Recovery_解题报告 题目出处 题意:输入两组数据,分别是前序遍历序列和中序遍历序列,你需要编写程序通过这两组数据求出该树的后序遍历序列(前序序列 + 中序序列 = 后序序列) 解法:递归 题目分析: 可以先按照用笔和纸的形式去推导出后序序列。推导过程省略,在推导过程中我们会发现规律: 假设 前序序列是 A B E H F C G I  中序序列是 H E B F A C I G (图如下) 每一 阅读详情


Tree Recovery

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 15363

Accepted: 9510

Description

Little Valentineliked playing with binary trees very much. Her favorite game was constructingrandomly 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 eachtree: a preorder traversal (root, left subtree, right subtree) and an inordertraversal (left subtree, root, right subtree). For the tree drawn above thepreorder traversal is DBACEGF and the inorder traversal is ABCDEFG. 
She thought that such a pair of strings would give enough information toreconstruct the tree later (but she never tried it). 

Now, years later, looking again at the strings, she realized thatreconstructing the trees was indeed possible, but only because she never hadused 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! 

Input

The input will contain one or more testcases. 
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 than26 characters.) 
Input is terminated by end of file. 

Output

For each testcase, recover Valentine's binary tree and print one line containing the tree'spostorder traversal (left subtree, right subtree, root).

Sample Input

DBACEGF ABCDEFG

BCAD CBAD

Sample Output

ACBFGED

CDAB

 

 

思路:先建树在后序遍历:

建树

 

 

代码:

class TreeNode{
public:
    char val;
    TreeNode*left,*right;
    TreeNode(char val){this->val= val;}
 
    static TreeNode*buildTree(vector<char>&preorder, vector<char>&inorder){
        map<char,int> map;
       for(int i=0; i<inorder.size(); i++)
            map[inorder[i]]= i;
       return helper(0,0, inorder.size()-1, preorder, inorder,map);
    }
    static TreeNode*helper(int preStart,int inStart,int inEnd,
        vector<char>&preorder, vector<char>&inorder, map<char,int>&map){
       if(preStart>= preorder.size()|| inStart>inEnd)
           returnNULL;
        TreeNode*root=new TreeNode(preorder[preStart]);
       int inIndex= map[preorder[preStart]];
        root->left= helper(preStart+1, inStart, inIndex-1,
            preorder, inorder, map);
        root->right= helper(preStart+ inIndex- inStart+1,
            inIndex+1, inEnd, preorder, inorder, map);
       return root;
    }
}

 

 

后序遍历代码:

static vector<char> postOrderTravel(TreeNode*root){
        vector<char>*v=new vector<char>;
        postOrderTravel(root,*v);
       return*v;
    }
    staticvoid postOrderTravel(TreeNode*root, vector<char>&v){
       if(root==NULL)return;
       if(root->left!=NULL) postOrderTravel(root->left, v);
       if(root->right!=NULL) postOrderTravel(root->right, v);
        v.push_back(root->val);
    }
    vector<char> postOrderTravel(){
       return postOrderTravel(this);
    }

 


完整代码:

//POJ 2255 Tree Recovery
#include <vector>
#include <string>
#include <iostream>
#include <map>
usingnamespace std;
 
class TreeNode{
public:
    char val;
    TreeNode *left,*right;
    TreeNode(char val){ this->val = val;}
 
    static TreeNode*buildTree(vector<char>&preorder, vector<char>&inorder){
        map<char,int> map;
        for(int i= 0; i<inorder.size(); i++)
            map[inorder[i]]= i;
        return helper(0,0, inorder.size()- 1, preorder, inorder,map);
    }
    static TreeNode*helper(int preStart,int inStart,int inEnd,
        vector<char>&preorder, vector<char>&inorder, map<char,int>&map){
        if(preStart >= preorder.size()|| inStart>inEnd)
            returnNULL;
        TreeNode *root= new TreeNode(preorder[preStart]);
        int inIndex= map[preorder[preStart]];
        root->left= helper(preStart+ 1, inStart, inIndex- 1,
            preorder, inorder, map);
        root->right= helper(preStart+ inIndex - inStart+ 1,
            inIndex+1, inEnd, preorder, inorder, map);
        return root;
    }
    static vector<char> postOrderTravel(TreeNode*root){
        vector<char>*v =new vector<char>;
        postOrderTravel(root,*v);
        return*v;
    }
    staticvoid postOrderTravel(TreeNode*root, vector<char>&v){
        if(root ==NULL)return;
        if(root->left!= NULL) postOrderTravel(root->left, v);
        if(root->right!= NULL) postOrderTravel(root->right, v);
        v.push_back(root->val);
    }
    vector<char> postOrderTravel(){
        return postOrderTravel(this);
    }
 
};
 
int main(int argc,char const *argv[])
{  
    //freopen("in.txt","r", stdin);
    string str;
    while(cin >> str){
        vector<char> preorder(str.begin(), str.end());
        cin >> str;
        vector<char> inorder(str.begin(), str.end());
        TreeNode *root= TreeNode::buildTree(preorder, inorder);
        vector<char> ans= root->postOrderTravel();
        for(int i= 0; i<ans.size(); i++) cout << ans[i];
        cout << endl;
    }
    return0;
}

 



WSL Ubuntu20 通过1Panel配置SSH服务的完整避坑指南 本文详细介绍了在WSL Ubuntu20环境下通过1Panel配置SSH服务的完整指南,包括环境准备、SSH服务安装与基础配置、1Panel中的SSH管理技巧以及连接测试与故障排查。特别针对WSL特性提供了避坑建议,如IP动态变化处理、SSH服务初始化配置等关键步骤,帮助用户高效搭建稳定的SSH服务环境。 阅读详情

相关推荐

STM32+LoRa模块实战:从环境搭建到数据传输完整指南(附避坑清单)

本文提供STM32与LoRa模块从环境搭建到数据传输的完整实战指南,涵盖硬件连接、开发环境配置、AT指令设置、频段优化及低功耗设计等关键步骤,特别针对常见问题提供解决方案和避坑清单,助力物联网开发者高效实现远距离低功耗通信。

weixin_29168813的博客 289

POJ 刷题系列:2255. Tree Recovery

POJ 刷题系列:2255. Tree Recovery传送门:POJ 2255. Tree Recovery题意: 给出先序和中序,求后序。 思路: 依旧很水,给定先序易知根结点,从中序中找到根结点就能确定左孩子们和右孩子们,递归建树。最后后序遍历一遍即可。代码如下:import java.io.BufferedReader; import java.io.File; import java

Demon-初来驾到 504

Tomcat启动过程中cmd窗口(控制台)中文乱码的问题

tomcat启动时,启动过程日志中文乱码的处理方法。

寻码启示 1942

POJ-2255 Tree Recovery

POJ-2255 Tree Recovery 题目链接:POJ-2255 题目大意:给定一棵树的前序遍历和中序遍历,让你输出这颗树的后序遍历 解题思路:二分法的思想???? 代码块: #include<iostream> #include<string> using namespace std; string pre, mid; int n = -1; void s...

鱼头的水题之路 223

AOSP Android14 Launcher3——动画核心类QuickstepTransitionManager详解

是一个复杂但核心的动画管理器。它利用 Android 的远程动画机制,拦截系统默认的窗口切换,并替换为高度定制、与 Launcher UI 深度集成的平滑过渡动画。当用户从一个应用程序通过手势或按键返回到 Launcher (桌面) 时,它负责创建和运行动画,使得应用程序窗口能够平滑地过渡(通常是缩小)到 Launcher 上的对应目标(图标、小部件或屏幕中心),同时 Launcher 界面元素(工作区、背景等)恢复显示。计算动画所需的各种参数,如窗口的起始/结束位置、缩放比例、透明度、圆角、裁剪区域等。

Micheal_Yang's Blog 2667

WeDetect:基于视觉-语言检索的目标检测新范式

目标检测正从传统回归与分类耦合范式,向开放、灵活、可扩展的多模态检索范式演进。检索式检测(Retrieval-based Detection)将检测任务重构为视觉特征与文本语义的联合匹配过程,依托候选框索引库与双塔架构,天然支持开放词汇、零样本识别与小目标定位。其技术价值在于解耦空间感知与语义理解,消除NMS与anchor匹配等冗余计算,显著提升推理效率与跨场景泛化能力。典型应用场景包括工业质检中的缺陷新增识别、遥感图像小目标检测、低光照鲁棒视觉系统等。WeDetect正是这一范式的代表性实现,深度融合视觉

weixin_30878501的博客 361

poj 2255 Tree Recovery

二叉树,给前序中序求后序

繁华过后你还有我 950

基于遗传算法的柔性作业车间调度(初始化以及编码策略)

这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入 欢迎使用Markdown编辑器 你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Mar

菜鸡 8594

poj2255 Tree Recovery

Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11955   Accepted: 7505 Description Little Valentine liked playing with binary trees very much

Jinbao 652

论文解读--Phase Noise in FMCW Radar Systems

相位噪声是现代雷达、通信、光谱和计量系统的基本性能参数之一。本文提出了一种适用于FMCW雷达系统的相位噪声理论。导出了一个新的设计方程,用于确定雷达系统中允许的源相位噪声电平的最大界。分析了在传输延迟小于参考振荡器相干时间时,相干混频引起的非线性相位噪声去相关函数;讨论了在传输延迟大于参考振荡器相干时间时,目标响应的频谱展宽。提出了收发链中各子系统的影响,并讨论了一种新的ADC相位噪声模型。介绍了相位噪声建模技术,然后将锁相环频率合成器与低噪声频率合成器进行比较,以演示减少相位噪声边带以改善检测和跟踪性能。

奔袭的算法工程师 3766

​网格划分理论:CFD理论67 网格划分-三大理论问题_哔哩哔哩_bilibili传统解释非结构化网格是不对的——计算传热学大叔专题讨论2--CFD网格类型_哔哩哔哩_bilibili

传统解释非结构化网格是不对的——计算传热学大叔。fluent计算理论和离散方法原理视频推荐。

qq_15963745的博客 183

POJ2255-Tree Recovery

全解题报告索引目录 -> 【北大ACM – POJ试题分类】 转载请注明出处:http://exp-blog.com ------------------------------------------------------------------------- 提示:二叉树遍历而已。。。给出前序和中序,求后序 /* Author: Exp...

ζёСяêτ - 小優YoU 6350

【项目实战】DevOps制品管理之JFrog Artifactory介绍

JFrog Artifactory是一款领先的二进制软件制品仓库管理工具,由JFrog公司开发。

奶爸的编程之路,也就一周冷个三天 1352

Tree Recovery poj 2255 c++

Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11421   Accepted: 7167 Description Little Valentine liked playing with binary trees very much. He

u012938194的专栏 657

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

POJ2255 Tree Recovery 二叉树遍历

Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12523   Accepted: 7836 Description Little Valentine liked playing with binary trees very much.

Jonariguez 吾本优秀 1239

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

poj2255Tree Recovery

数据结构应该有讲,前序遍历和中序遍历 或者是中序遍历和后序遍历可以恢复整个二叉树,然后在建立利用另一种遍历 14272030 h123120 2255 Accepted 688K 0MS G++ 1278B 2015-06-06 17:18:47 #include #include #include #include #include #inc

h123120的专栏 490

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

Tree Recovery 【POJ - 2255】

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 creatio...

Meant_To_Be的博客 263

Tree Recovery POJ - 2255

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 creati...

Qtt110的博客 330

POJ 2255 Tree Recovery

二叉树的遍历,给出前序和中序遍历,输出后序遍历。

笨鸟 888

POJ 2255 Tree Recovery

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 creatio...

起名好难呀! 276

Poj 2255 Tree Recovery【dfs遍历树】

Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13448   Accepted: 8393 Description Little Valentine liked playing with binary trees very much

希望的专栏 987

二叉树的重建——POJ2255 Tree Recovery之篇2

继POJ2255之后纠结了很久。。,看了某本书后对于二叉树有了新的更深一点的认识。。。。   对于二叉树T,可以递归定义它的前序遍历、中序遍历和后序遍历如下: PreOrder(T)=T的根节点+PreOrder(T的左子树)+PreOrder(T的右子树); InOrder(T)=InOrder(T的左子树)+T的根节点+InOrder(T的右子树); PostOrder(T)=Pos

155
上一篇: poj 3006 Dirichlet's Theorem on Arithmetic Progressions
下一篇: poj 3094 Quicksum
朔北冥
博客等级 码龄11年 14粉丝 27原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值