poj2255
程序员文章站
2022-05-19 19:50:52
...
前序遍历+中序遍历转后序遍历。
#include<iostream>
#include<string>
using namespace std;
string s1, s2;
void build(int n, string s1, string s2)
{
if(n <= 0)
return;
int tmp = s2.find(s1[0]);
build(tmp, s1.substr(1, tmp), s2.substr(0, tmp));
build(n - tmp - 1, s1.substr(1 + tmp, n - tmp - 1), s2.substr(tmp + 1, n - tmp - 1));
cout << s1[0];
}
int main()
{
while(cin >> s1 >> s2)
{
build(s1.size(), s1, s2);
cout << endl;
}
// system("pause");
return 0;
}
上一篇: Tree Recovery poj2255(前序中序遍历建立二叉树)
下一篇: Java版的树