【题目描述】
Give you the Preorder enumeration and Inorder enumeration of a binary tree, output its Postorder enumeration. We assume the elements of binary tree are different.
【输入要求】
The first line contains the number of test cases, N.
In each tese case, the first line is the Preorder enumeration and the second line is Inorder enumeration.
1
ABDCEGFHI
BDAGECHFI
【输出要求】
Out put N lines. Each line represent the Postorder enumeration
DBGEHIFCA
【我的程序】
#include <iostream>
#include <string>
using namespace std;
int index;
string qian,zhong;
void enumeration(int x, int y)
{
if (x>y) return;
char root=qian[index++];
int pos=zhong.find(root,x);
enumeration(x,pos-1);
enumeration(pos+1,y);
cout<< root;
}
int main()
{
int n;
cin>> n;
cin.get();
for (int i=0;i<n;i++)
{
index=0;
getline(cin,qian);
getline(cin,zhong);
enumeration(0,zhong.length()-1);
cout<< endl;
}
return 0;
}
本文介绍了一种通过前序和中序遍历确定二叉树后序遍历的方法,并提供了一个C++实现示例。该算法适用于不同元素组成的二叉树。
910

被折叠的 条评论
为什么被折叠?



