POJ2255----Tree Recovery
来源:互联网 发布:eminem光明会 知乎 编辑:程序博客网 时间:2024/05/20 06:07
题目见此链接
此题主要考察二叉树,根据给出先序遍历和中序遍历求后序遍历。
代码如下:
#include <iostream>#include <string>//#include <fstream>using namespace std;class Node{ public: Node *left, *right; char data;};//AEFDHZMG//build treevoid build(string preord, string inord, Node *root){ root->data = preord[0]; if(preord.length() == 1) { return ; } int rootLoc = inord.find(preord[0]); int rightLoc = preord.find(inord[rootLoc+1]); //cout << rightLoc << endl; //建立左子树 if(rootLoc > 0) { root->left = new Node(); build(preord.substr(1, rootLoc), inord.substr(0, rootLoc), root->left); } //建立右子树 if(rightLoc > 0) { root->right = new Node(); build(preord.substr(rootLoc + 1), inord.substr(rootLoc+1), root->right); }}//后序遍历二叉树void postorder(Node *root){ if(root->left) { postorder(root->left); } if(root->right) { postorder(root->right); } cout << root->data;}int main(){ //ifstream in("data2255.in"); string preord, inord, postord; while (cin >> preord >> inord) { Node *root = new Node(); build(preord, inord, root); postorder(root); cout << endl; delete root; } return 0;}
1 0
- POJ2255 Tree Recovery
- poj2255 Tree Recovery
- poj2255 Tree Recovery
- POJ2255-Tree Recovery
- poj2255 Tree Recovery(递归)
- POJ2255----Tree Recovery
- POJ2255,Tree Recovery,二叉树重建
- poj2255 Tree Recovery 二叉树遍历
- poj2255~~Tree Recovery (树的遍历构造~)
- poj2255- Tree Recovery(二叉树)
- POJ2255 Tree Recovery 前序 中序求后序
- POJ2255 Tree Recovery 【树的遍历】
- POJ2255 Tree Recovery 二叉树遍历
- POJ2255 Tree Recovery (先序中序-》后序)
- POJ2255 Tree Recovery(二叉树遍历)
- 【POJ2255】Tree Recovery解题报告 思路 + 数据 +代码
- poj2255 Tree Recovery 二叉树,这题整我好惨
- poj2255——Tree Recovery(应用二叉树)
- 浅谈C/C++的编译问题
- C++学习笔记--多线程
- 机器学习算法比较
- 【Qt开发】事件循环与线程 二
- 中秋之夜 一个感人的flash动画小故事
- POJ2255----Tree Recovery
- Android与设计模式之Builder
- Cpp_Primer--函数
- uva 839 Not so Mobile 二叉树
- javaWeb中小问题,实体的DAO方法中定义实体对象问题
- MIT6.00 1x Lecture 2 - Core elements of programs 学习笔记
- JTAG的调试原理
- 推荐系统中所使用的混合技术介绍
- 给瀑布流加入模态对话框碰到的问题