根据中序序列和前序序列,求二叉树的后序序列
来源:互联网 发布:我国网络舆论的特点是? 编辑:程序博客网 时间:2024/05/22 01:43
/**//*
作者:shaoshaoh
日期:2006-10-01
声明:欢迎转载,请保留此信息
Problem C: Mr. Bean
Mr. Bean likes bean games, especially the "beanary tree" game. To enjoy a
beanary tree game, he plays the following steps:
l He firstly picks one bean from each of his N different kinds of beans
and randomly arranges them into a sequence, that is, a sequence of N
different beans. Mr. Bean calls this sequence the preorder sequence.
l Then, he picks another random bean sequence in the same way, called the
inorder sequence.(strange names, aren't they?)
l Now come to the difficult step. Mr. Bean picks yet another N different
beans and, this time, he tries to put them into…guess what, no no, not a
sequence, but a tree! A beanary-tree!
What is a beanary-tree? A beanary-tree is in fact a special kind of binary
tree of beans. Such tree has the magic property that if you picks its beans
in pre-order(recursively parent then left then right), the bean sequence
thus obtained is just the same as the preorder sequence he randomly picked
in the first step! Besides, if you pick the beans in in-order(recursively
left then parent then right), the bean sequence thus obtained is just the
same as the inorder sequence! What a magic.
l This is not the end of the game. Mr. Bean finally picks the beans in
postorder(recursively left then right then parent) and record the resulted
postorder sequence in his notebook.(sequences save space, unlike trees…)
This figure illustrates the preorder, inorder, postorder of a binary tree
The game is difficult. Sometimes it's even impossible to finish the
process. Usually, the old Bean found that he is too old for doing this, so
he asks you for help. Please write a clever program that could input the
preorder sequence and the inorder sequence and then play the rest of the
game. To make your life easier, you only need to output the corresponding
postorder sequence if possible.
Input Specification:
The input only contains two strings. The first string represents the
preorder sequence and the second string represents the inorder sequence.
Each of the strings consists only of capital letters. Different capital
letters represent different kinds of beans. You may assume that the input
is always meaningful for the game.
Output Specification:
In a single line, output a string representing the postorder sequence. If
it is impossible, output the string "-_-bbb"(no quotation mark should be
printed).
Sample Input 1:
KGCBDHQMPY
BCDGHKMPQY
Sample Output 1:
BDCHGPMYQK
Sample Input 2:
ABC
CAB
Sample Output 2:
-_-bbb
*/
#include <stack>
#include <iostream>
using namespace std;
char pre[100], in[100];
stack<char> tt;
bool flag;
int search(char* arr, char key, int s, int e)
...{
int pos = s;
while(arr[pos]!=key && pos<=e)
...{
pos++;
};
if (pos>e) return -1;
else return pos;
}
void pushall(char *pre, int ps, int pe, char *in, int ins, int ine)
//给出一个前序序列,给出一个中序序列,然后根据中序和前序,按照后序的
//逆序,将队列压栈。也即先将根节点压栈,然后对于右子树和左子树递归
//调用此过程。最后实现将全部节点值压栈,逐个弹出就是后序遍历的结果
...{
flag = false;
tt.push(pre[ps]);
if (ps == pe)
...{
if (pre[ps] == in[ins])
flag = true;
return;
}
int i = search(in, pre[ps], ins, ine);
if (i==-1) return;
//如果存在右子树,递归
if (ps+(i-ins) < pe) pushall(pre,ps+(i-ins)+1,pe,in,i+1,ine);
//如果存在左子树,递归
if (i-ins > 0) pushall(pre,ps+1,ps+(i-ins),in, ins, i-1);
}
void main(void)
...{
flag = false;
cout<<"Please input the preorder list"<<endl;
cin>>pre;
cout<<"Please input the inorder list"<<endl;
cin>>in;
int len = 0;
while(pre[len]!=0)
...{
len++;
}
pushall(pre,0,len-1,in,0,len-1);
if (!flag)
...{
cout<<"-_-bbb"<<endl;
return;
}
while(!tt.empty())
...{
cout<<tt.top();
tt.pop();
}
cout<<endl;
}
作者:shaoshaoh
日期:2006-10-01
声明:欢迎转载,请保留此信息
Problem C: Mr. Bean
Mr. Bean likes bean games, especially the "beanary tree" game. To enjoy a
beanary tree game, he plays the following steps:
l He firstly picks one bean from each of his N different kinds of beans
and randomly arranges them into a sequence, that is, a sequence of N
different beans. Mr. Bean calls this sequence the preorder sequence.
l Then, he picks another random bean sequence in the same way, called the
inorder sequence.(strange names, aren't they?)
l Now come to the difficult step. Mr. Bean picks yet another N different
beans and, this time, he tries to put them into…guess what, no no, not a
sequence, but a tree! A beanary-tree!
What is a beanary-tree? A beanary-tree is in fact a special kind of binary
tree of beans. Such tree has the magic property that if you picks its beans
in pre-order(recursively parent then left then right), the bean sequence
thus obtained is just the same as the preorder sequence he randomly picked
in the first step! Besides, if you pick the beans in in-order(recursively
left then parent then right), the bean sequence thus obtained is just the
same as the inorder sequence! What a magic.
l This is not the end of the game. Mr. Bean finally picks the beans in
postorder(recursively left then right then parent) and record the resulted
postorder sequence in his notebook.(sequences save space, unlike trees…)
This figure illustrates the preorder, inorder, postorder of a binary tree
The game is difficult. Sometimes it's even impossible to finish the
process. Usually, the old Bean found that he is too old for doing this, so
he asks you for help. Please write a clever program that could input the
preorder sequence and the inorder sequence and then play the rest of the
game. To make your life easier, you only need to output the corresponding
postorder sequence if possible.
Input Specification:
The input only contains two strings. The first string represents the
preorder sequence and the second string represents the inorder sequence.
Each of the strings consists only of capital letters. Different capital
letters represent different kinds of beans. You may assume that the input
is always meaningful for the game.
Output Specification:
In a single line, output a string representing the postorder sequence. If
it is impossible, output the string "-_-bbb"(no quotation mark should be
printed).
Sample Input 1:
KGCBDHQMPY
BCDGHKMPQY
Sample Output 1:
BDCHGPMYQK
Sample Input 2:
ABC
CAB
Sample Output 2:
-_-bbb
*/
#include <stack>
#include <iostream>
using namespace std;
char pre[100], in[100];
stack<char> tt;
bool flag;
int search(char* arr, char key, int s, int e)
...{
int pos = s;
while(arr[pos]!=key && pos<=e)
...{
pos++;
};
if (pos>e) return -1;
else return pos;
}
void pushall(char *pre, int ps, int pe, char *in, int ins, int ine)
//给出一个前序序列,给出一个中序序列,然后根据中序和前序,按照后序的
//逆序,将队列压栈。也即先将根节点压栈,然后对于右子树和左子树递归
//调用此过程。最后实现将全部节点值压栈,逐个弹出就是后序遍历的结果
...{
flag = false;
tt.push(pre[ps]);
if (ps == pe)
...{
if (pre[ps] == in[ins])
flag = true;
return;
}
int i = search(in, pre[ps], ins, ine);
if (i==-1) return;
//如果存在右子树,递归
if (ps+(i-ins) < pe) pushall(pre,ps+(i-ins)+1,pe,in,i+1,ine);
//如果存在左子树,递归
if (i-ins > 0) pushall(pre,ps+1,ps+(i-ins),in, ins, i-1);
}
void main(void)
...{
flag = false;
cout<<"Please input the preorder list"<<endl;
cin>>pre;
cout<<"Please input the inorder list"<<endl;
cin>>in;
int len = 0;
while(pre[len]!=0)
...{
len++;
}
pushall(pre,0,len-1,in,0,len-1);
if (!flag)
...{
cout<<"-_-bbb"<<endl;
return;
}
while(!tt.empty())
...{
cout<<tt.top();
tt.pop();
}
cout<<endl;
}
- 根据中序序列和前序序列,求二叉树的后序序列
- 根据二叉树的前序序列和中序序列得到后序序列
- 根据二叉树的前序遍历序列和中序遍历序列求二叉树的后序遍历序列
- 已知二叉树的前序序列和中序序列,求二叉树的后序序列
- 根据树的后序和中序序列或者前序和中序序列构建二叉树,
- 知道前序序列和后序序列求二叉树的个数+大数
- 二叉树序列---根据后序和中序系列或前序和中序序列构造二叉树
- 已知前序遍历序列和中序遍历序列,求二叉树的后序遍历
- 根据树的前序和中序序列求出后序序列
- 根据二叉树的先序序列和中序序列还原二叉树并打印后序序列
- 根据前序和中序序列重建二叉树
- 根据前序和中序序列确定二叉树
- 根据前序和中序列 重建二叉树
- 已知二叉树的中序序列和前序序列(或后序)求解树
- 已知二叉树的中序序列和前序序列(或后序)求解树
- 根据先序和中序序列求二叉树
- 【树】根据前序序列和中序序列写出二叉树
- 根据前序遍历序列和中序遍历序列构造二叉树
- H.264标准中的分层技术
- 求第n个Fibonacci数
- 快速排序算法
- 引爆地雷
- 用C#设置系统时间和本地时间
- 根据中序序列和前序序列,求二叉树的后序序列
- 初识Linux桌面环境
- 回溯法的一个经典实例-n皇后问题
- 在Linux下办公、装软件
- Linux的上网工具——Mozilla
- 菜鸟也装Linux
- XmlSerializer 序列化
- 从硬盘安装Rad Hat9.0
- Linux资源无忧