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;
}

 



避开生产计划大坑:不懂MPS和MRP的区别,你的SAP PP模块白学了 本文深入解析SAP PP模块中MPS(主生产计划)与MRP(物料需求计划)的核心区别,揭示如何通过正确配置避免生产计划中的常见陷阱。文章详细探讨了MPS的三大缓冲特性和动态平衡算法,帮助企业提升生产计划稳定性,优化资源配置,避免生产线波动。 阅读详情

相关推荐

STM32版CCD线性摄像头寻线寻迹小车_stm32接ccd

目前大多数的小车寻线寻迹都是用红外对管寻线,这是比较简单也比较成熟的技术方案,且成本也低。本文将介绍使用CCD线性摄像头寻线寻迹。TSL1401 线性传感器由一个 1x128 的光电二极管阵列、相关的电荷放大电路以及一个内部像素数据保功能组成。内部像素数据保功能可以为所有像素点提供同时积分的开始和停止时间。该阵列由 128 个像素组成,每个像素的感光面积为 3,524.3 平方微米。像素之间的间隔为 8μm。内部控制逻辑简化了操作,该模块需要串行输入(SI)信号和时钟信号(CLK)。

2401_87557129的博客 3228

POJ 刷题系列:2255. Tree Recovery

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

Demon-初来驾到 504

速腾R128雷达+海康全景相机ROS数据采集避坑实录:从‘雷达不转’到MATLAB标定的完整流程

本文详细记录了速腾R128激光雷达与海康威视全景相机在ROS环境下的数据采集与MATLAB标定全流程。从硬件配置的常见陷阱(如雷达旋转参数设置)、驱动编译的依赖管理,到多传感器同步的实战技巧和MATLAB标定方法,提供了完整的避坑指南和技术方案。特别针对RoboSense R128雷达和海康相机的特殊需求给出了具体解决方案。

weixin_28717807的博客 1218

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

【VisualStdio】手把手教你做一个上位机(1)控件设计布局

网上有很多形形色色的制作上位机教程,有些写的挺好的,但是过于单调。

m0_73409202的博客 5435

智能温室:温室环境监测_(7).土壤监测技术包括pH值和电导率

土壤的pH值是指土壤溶液的酸碱度,它对植物的营养吸收和生长发育有着重要的影响。在智能温室中,土壤监测是非常重要的一个环节,因为土壤的pH值和电导率直接影响植物的生长和健康。本节将详细介绍如何利用传感器和数据处理技术来监测土壤的pH值和电导率,并提供具体的代码示例。在智能温室中,监测到的土壤pH值和电导率数据需要传输到中央控制系统进行进一步的处理和分析。通过以上步骤,您可以构建一个完整的智能温室土壤监测系统,实时监控土壤的pH值和电导率,确保植物在最佳的生长环境中。校准通常包括在已知电导率的标准溶液中进行。

zhubeibei168的博客 1408

poj 2255 Tree Recovery

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

繁华过后你还有我 950

【AI 编程工具】Codex配置部署指南

目录一、环境准备与基础安装1. 系统兼容性检查2. 前置依赖安装3. 安装方式选择二、API密钥配置与认证1. 获取API密钥2. 配置文件设置3. 临时配置方式三、沙盒环境配置与安全策略1. 沙盒模式选择2. 安全最佳实践四、高级功能配置1. 模型上下文协议(MCP)配置2. 模型版本切换3. 自定义网关配置五、常见问题排查1. 安装失败2. API调用失败3. 处理速度慢4. 配置文件不生效六、生产环境部署建议macOS/Linux:官方提供稳定支持,推荐使用原生环境Windows:官方标注为实验性支持

以实战经验介绍行业知识 2886

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

基于物联网的门禁与考勤系统(阿里云)_81

本课题设计的基于物联网的门禁与考勤管理系统,结合现代信息技术,利用指纹识别、射频卡、密码输入和远程控制等多种开锁方式,提升门禁系统的安全性和灵活性;同时,通过物联网平台进行数据上传和远程管理,极大提高了管理效率。通过智能化的考勤功能,系统可以实时记录员工的出入情况,并通过云平台进行远程查询和管理,推动门禁与考勤管理的数字化、智能化和高效化。

1万+

YOLO + SubspaceAD:一张良品图,检出所有未知缺陷

当YOLO遇上子空间建模,工业质检正迎来从“见多识广”到“举一反三”的质变时刻。

①答疑群聊服务;②YOLO大模型知识问答系统;③计算机视觉论文生成智能体; 495

POJ2255-Tree Recovery

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

ζёСяêτ - 小優YoU 6350

如何正确的计算两个角度的角度差?

这一步是必要的,因为它确保了无论a相对于b是在逆时针方向还是顺时针方向,d2总是正确地反映出绕圆相反方向的角度差。这样,当函数进行最后的比较时,它能够准确地比较两个方向的旋转路径并选择最短的一个。

小秋 AI SLAM 入门实战教程 4136

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

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

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

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

poj2255Tree Recovery

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

h123120的专栏 490

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

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、付费专栏及课程。

余额充值