POJ 2255 Tree Recovery(还原树)
2015-12-23 20:01
507 查看
题目链接:POJ 2255
题意:
给出二叉树的前序和中序遍历求出后序遍历。
分析:
递归。
题意:
给出二叉树的前序和中序遍历求出后序遍历。
分析:
递归。
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <fstream> #include <algorithm> using namespace std; struct TreeNode { struct TreeNode* left; struct TreeNode* right; char elem; }; void BinaryTreeFromOrderings(char* inorder, char* preorder, int length) { if (length == 0) return; TreeNode* node = new TreeNode; node->elem = *preorder; int rootIndex = 0; for (;rootIndex < length;rootIndex++) if (inorder[rootIndex] == *preorder) break; BinaryTreeFromOrderings(inorder, preorder + 1, rootIndex); BinaryTreeFromOrderings(inorder + rootIndex + 1, preorder + rootIndex + 1, length - (rootIndex + 1)); cout << node->elem; return; } int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif char in[27], pre[27]; int len; while (cin >> pre >> in) { len = strlen(pre); BinaryTreeFromOrderings(in, pre, len); cout << endl; } return 0; }
相关文章推荐
- Python数据分析入门之pandas基础总结
- (NO.00005)iOS实现炸弹人游戏(三):从主场景类谈起
- (NO.00005)iOS实现炸弹人游戏(三):从主场景类谈起
- (NO.00005)iOS实现炸弹人游戏(三):从主场景类谈起
- python 运行不过去SyntaxError: Non-ASCII character '\xc2' in file
- Git SSH Key 生成步骤
- 响应式布局
- 拖动条Seek Bar
- sizeof与offsetof有关的结构体详解(转自吉米歪)
- 文件2(转)
- nodejs eventEmitter源码解读
- 文件操作(转)
- 手机app测试分析方法 -- 元素分析法(黑盒测试分析法)
- View中OnClickListener对onTouch的影响
- 移动开发成长路线
- 二叉树——前序、中序遍历得后序遍历
- Toast弹出信息
- synchronized的使用方法
- Oracle体系结构(三)---DBWR机制
- 9种CSS3 blend模式制作的鼠标滑过图片标题特效