#include<iostream>
using namespace std;
struct TreeNode {
char data;
TreeNode* l;
TreeNode* r;
};
TreeNode* pre(TreeNode* r, string in, string last) {
if (in.size() == 0) {
return nullptr;
}
char ch = last[last.size() - 1];
r = new TreeNode;
r->data = ch;
r->l = r->r = nullptr;
int x = in.find(ch);
r->l = pre(r->l, in.substr(0, x), last.substr(0, x));
r->r = pre(r->r, in.substr(x + 1), last.substr(x, last.size() -x- 1));
return r;
}
void preOrder(TreeNode* r) {
if (r != nullptr) {
cout << r->data;
}
if (r == nullptr) {
return;
}
else {
preOrder(r->l);
preOrder(r->r);
}
}
int main() {
TreeNode* root=nullptr;
string in = "ACGDBHZKX";
string out = "CDGAHXKZB";
root = pre(root, in, out);
preOrder(root);
return 0;
}
83

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



