POJ2255Tree Recovery(二叉樹)
Tree Recovery
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11241 | Accepted: 7048 |
Description
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!
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!
Input
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.
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.
Output
For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root).
Sample Input
DBACEGF ABCDEFG BCAD CBAD
Sample Output
ACBFGED CDAB
題目大意:告訴你一個二叉樹的前序遍歷,中序遍歷,求它的後序遍歷
基礎知識:前序遍歷 根左右,中序遍歷 左根右, 後序遍歷 左右根
前序遍歷的第一個 後序遍歷的最後一個一定為根
因此我們從前序遍歷入手 在中序遍歷中找到跟的位置 然後將跟拿走 剩下 左子樹和右子樹 剩下的依然是兩個樹 繼續進行遞迴 然後得到整個的後續遍歷
例如
前序DBACEGF S1 中序ABCDEFG S2
第一個跟為D 在s2中找到D 剔除 剩下 ABC EFG 分別為左右子樹
然後再左子樹中找到B 左子樹中存在 左子樹中剩下 A,C分別為左右子樹 遞迴完畢
在右子樹中進行遞迴 首先在s1中找到 E 然後剩下FG 都在E的右子樹上 然後S1中搜到 G 剩下F,參照S2可知 為G的左兒子。
程式碼如下:
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 30;
char pre[N],mid[N];
int n;
void make_tree(int l,int r)
{
int k;
if(l>r)
return;
for(k=l;k<=r;k++)
if(pre[n]==mid[k])
break;
n++;
make_tree(l,k-1);//建左子樹:順序不可顛倒
make_tree(k+1,r);//建右子樹;
cout<<mid[k];
}
int main()
{
while(cin>>pre>>mid){
n=0;
int len=strlen(pre)-1;
make_tree(0,len);
puts("");
}
return 0;
}
相關文章
- 滿二叉樹、完全二叉樹、平衡二叉樹、二叉搜尋樹(二叉查詢樹)和最優二叉樹二叉樹
- 二叉樹 & 二叉查詢樹二叉樹
- 排序二叉樹和平衡二叉樹排序二叉樹
- 二叉查詢樹(二叉排序樹)排序
- 二叉樹(順序儲存二叉樹,線索化二叉樹)二叉樹
- 手擼二叉樹——二叉查詢樹二叉樹
- 手擼二叉樹——AVL平衡二叉樹二叉樹
- 資料結構之樹結構概述(含滿二叉樹、完全二叉樹、平衡二叉樹、二叉搜尋樹、紅黑樹、B-樹、B+樹、B*樹)資料結構二叉樹
- 二叉樹二叉樹
- 二叉樹的應用(1)--二叉樹排序樹基本操作二叉樹排序
- 判斷二叉樹是否為滿二叉樹二叉樹
- 資料結構中的樹(二叉樹、二叉搜尋樹、AVL樹)資料結構二叉樹
- ZOJ Problem Set - 1944 Tree Recovery(二叉樹三種遍歷知二求三)二叉樹
- 自己動手作圖深入理解二叉樹、滿二叉樹及完全二叉樹二叉樹
- 二叉樹、B樹以及B+樹二叉樹
- 平衡二叉樹,B樹,B+樹二叉樹
- 深入學習二叉樹 (一) 二叉樹基礎二叉樹
- 相同二叉樹和鏡面二叉樹問題二叉樹
- 判斷某棵二叉樹是否二叉排序樹二叉樹排序
- 平衡二叉樹(AVL樹)和 二叉排序樹轉化為平衡二叉樹 及C語言實現二叉樹排序C語言
- 樹和二叉樹簡介二叉樹
- n叉樹vs二叉樹二叉樹
- Chapter 3 樹與二叉樹APT二叉樹
- 樹(2)--二叉樹的遍歷(非遞迴)+線索二叉樹二叉樹遞迴
- 迭代二叉樹二叉樹
- 二叉樹深度二叉樹
- 重建二叉樹二叉樹
- javascript二叉樹JavaScript二叉樹
- 平衡二叉樹二叉樹
- 二叉樹---深度二叉樹
- Java二叉樹Java二叉樹
- JS二叉樹JS二叉樹
- 二叉排序樹排序
- 二叉樹——高度二叉樹
- 二叉蘋果樹蘋果
- 滿二叉樹二叉樹
- 12、二叉樹二叉樹
- 二叉樹的子結構、深度以及重建二叉樹二叉樹