习题课第一次作业:2、重建二叉树
2016-09-19 19:20
435 查看
描述:
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 creations:
To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG.
She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).
Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used 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!
输入
The input will contain one or more test cases.
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 than 26 characters.)
Input is terminated by end of file.
输出
For each test case, recover Valentine’s binary tree and print one line containing the tree’s postorder traversal (left subtree, right subtree, root).
样例输入
DBACEGF ABCDEFG
BCAD CBAD
样例输出
ACBFGED
CDAB
来源
Ulm Local 1997
解决思路:
//考虑中序与后序、中序与层次重构二叉树的算法
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 creations:
D / \ / \ B E / \ \ / \ \ A C G / / F
To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG.
She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).
Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used 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!
输入
The input will contain one or more test cases.
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 than 26 characters.)
Input is terminated by end of file.
输出
For each test case, recover Valentine’s binary tree and print one line containing the tree’s postorder traversal (left subtree, right subtree, root).
样例输入
DBACEGF ABCDEFG
BCAD CBAD
样例输出
ACBFGED
CDAB
来源
Ulm Local 1997
解决思路:
#include <iostream> #include <string> #define LOCAL using namespace std; string s_pre , s_in , s_post; struct Dnode { char ch; struct Dnode * left; struct Dnode * right; }; void Post_travle(Dnode * &T) { if ( T != nullptr ) { Post_travle(T->left); Post_travle(T->right); cout << T->ch ; delete(T); } } int Find_root(string in, char ch) { for (size_t i = 0 ;i < in.length(); i++) if (in[i] == ch) return i; return -1; } Dnode* Construct_Binary_tree(const string pre, int pre_low, int pre_high, const string in, int in_low, int in_high) { int len = in_high - in_low + 1; if (len > 0) { Dnode * cur = new Dnode; cur->ch = pre[pre_low]; cur->left = nullptr; cur->right = nullptr; if (len == 1) return cur; int postion = Find_root(in, cur->ch); cur->left = Construct_Binary_tree(pre, pre_low + 1, pre_low + postion - in_low, in, in_low, postion - 1); cur->right = Construct_Binary_tree(pre, pre_low + postion - in_low + 1, pre_high, in, postion + 1, in_high); return cur; } return nullptr; } int main() { #ifdef LOCAL freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif while ( std::cin >> s_pre >> s_in ) { Dnode * root = Construct_Binary_tree(s_pre, 0, s_pre.length() - 1, s_in, 0, s_in.length() - 1); Post_travle(root); cout << endl; } return 0; }
//考虑中序与后序、中序与层次重构二叉树的算法
相关文章推荐
- bzoj4491奇技淫巧线段树
- java中,使用zxing生成二维码
- mysql导入数据出现Errcode: 2 - No such file or directory错误信息
- LeetCode--16. 3Sum Closest
- hdu5890(二维01背包+bieset优化)
- leetcode 400. Nth Digit
- cocosCreate 黄金矿工绳索摆动,以及钩子的扔出和回收
- [ CodeVS冲杯之路 ] P3117
- Storage System and File System Courses
- char *a 与char a[] 的区别和char** argv与char *argv[]区别
- win64环境下sklearn的配置
- php的轻量级rpc框架yar
- GitHub与CocoaPods的使用
- 【effective c++】设计与声明
- 49. Group Anagrams
- input 的onChang失效问题
- Ubuntu16.04 操作
- HYSBZ 2243 染色 树链剖分 点上剖分
- python学习之 range,列表生成式与切片
- HDU 5887 Herbs Gathering 01背包+DFS搜索 *