Pat链接
经典题型
- 常规的建树法
#include<iostream>//AC
#include<vector>
#include<queue>
using namespace std;
const int maxn = 50;
int N;
int post[maxn], in[maxn];
struct node {
int value;
node* left;
node* right;
}tree[maxn];
node* Creattree(int pl, int pr, int il, int ir) {
if (pl > pr) { return NULL; }
node* t = new node;
t->value = post[pr];
int p = 0;
for (p = il; p <= ir; p++) { if (in[p] == post[pr]) { break; } }
t->left = Creattree(pl, pl + p - il - 1, il, p - 1);
t->right = Creattree(pl + p - il, pr - 1, p + 1, ir);
return t;
}
queue<node*> q;
vector<int> ans;
void getLevelOrder(node* root) {
q.push(root);
node* t;
while (!q.empty()) {
t = q.front();
q.pop();
ans.push_back(t->value);
if (t->left != NULL) { q.push(t->left); }
if (t->right != NULL) { q.push(t->right); }
}
}
int main() {
cin >> N;
for (int i = 0; i < N; i++) { cin >> post[i]; }
for (int i = 0; i < N; i++) { cin >> in[i]; }
node* root = NULL;
root = Creattree(0, N - 1, 0, N - 1);
getLevelOrder(root);
for (int i = 0; i < N; i++) {
cout << ans[i];
if (i != N - 1) { cout << ' '; }
else { cout << endl; }
}
return 0;
}
- 柳神方法
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct node {
int index, value;
};
bool cmp(node a, node b) {
return a.index < b.index;
}
vector<int> post, in;
vector<node> ans;
void pre(int root, int start, int end, int index) {
if (start > end) return;
int i = start;
while (i < end && in[i] != post[root]) i++;
ans.push_back({ index, post[root] });
pre(root - 1 - end + i, start, i - 1, 2 * index + 1);//把index项换成index+1
pre(root - 1, i + 1, end, 2 * index + 2);//把index项换成index+1,来表示层数,因为总是先访问左子树,故排序后相同层数先输出左边,但是这样最后一个点过不去
}
int main() {
int n;
scanf("%d", &n);
post.resize(n);
in.resize(n);
for (int i = 0; i < n; i++) scanf("%d", &post[i]);
for (int i = 0; i < n; i++) scanf("%d", &in[i]);
pre(n - 1, 0, n - 1, 0);
sort(ans.begin(), ans.end(), cmp);
for (int i = 0; i < ans.size(); i++) {
if (i != 0) cout << " ";
cout << ans[i].value;
}
return 0;
}
类似题型:
- 根据后序和中序输出前序:
#include <cstdio>
using namespace std;
int post[] = {3, 4, 2, 6, 5, 1};
int in[] = {3, 2, 4, 1, 6, 5};
void pre(int root, int start, int end) {//分别为根节点在后序的位置、中序起始点、中序末尾点
if(start > end) return ;
int i = start;
while(i < end && in[i] != post[root]) i++;
printf("%d ", post[root]);
pre(root - 1 - end + i, start, i - 1);//遍历左子树
pre(root - 1, i + 1, end);//遍历右子树
}
int main() {
pre(5, 0, 5);
return 0;
}
根据前序和中序输出后序序列的第一个值
1138 Postorder Traversal (25分)
- 我的代码
#include<iostream>
#include<vector>
using namespace std;
const int maxn = 5 * 1e4 + 10;
int N;
int pre[maxn], in[maxn];
vector<int> ans;
void fun(int root, int il, int ir, int index) {
if (il == ir) { ans.push_back(pre[root]); }//第一个没有子树的点即为后序序列的第一个点
if (il > ir) { return; }
int p = il;
while (p <= ir && in[p] != pre[root]) { p++; }
fun(root + 1, il, p - 1, index + 1);
fun(root + (p - il) + 1, p + 1, ir, index + 1);
}
int main() {
cin >> N;
for (int i = 0; i < N; i++) { cin >> pre[i]; }
for (int i = 0; i < N; i++) { cin >> in[i]; }
fun(0, 0, N - 1, 0);
cout << ans[0];
return 0;
}
- 柳神代码
#include <iostream>
#include <vector>
using namespace std;
vector<int> pre, in;
bool flag = false;
void postOrder(int prel, int inl, int inr) {
if (inl > inr || flag == true) return;//节省时间
int i = inl;
while (in[i] != pre[prel]) i++;
postOrder(prel + 1, inl, i - 1);
postOrder(prel + i - inl + 1, i + 1, inr);
if (flag == false) {//第一个退出两个递归的即为所求
printf("%d", in[i]);
flag = true;
}
}
int main() {
int n;
scanf("%d", &n);
pre.resize(n);
in.resize(n);
for (int i = 0; i < n; i++) scanf("%d", &pre[i]);
for (int i = 0; i < n; i++) scanf("%d", &in[i]);
postOrder(0, 0, n - 1);
return 0;
}