题目AC代码
思路没什么好说的,重点理解那两行遍历的递归就是了,卡在那很久,注意一个左子树的个数减去右边的子树个数这类的,附参考柳神的
~https://www.liuchuo.net/archives/2090
理解了这个这一类就ok了,重点理解那个递归的过程!!!!!!!!
#include<iostream>
#include<vector>
#include<map>
using namespace std;
vector<int>post, in;
map<int, int>p;
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++;
p[index] = post[root];
pre(root-end+i-1, start, i - 1, 2 * index + 1);
pre(root - 1, i + 1, end, 2 * index + 2);
}
int main()
{
int n; cin >> n;
int i;
for (int i = 0; i < n; i++)
{
int tmp; cin >> tmp;
post.push_back(tmp);
}
for (int i = 0; i < n; i++)
{
int tmp; cin >> tmp;
in.push_back(tmp);
}
pre(n-1, 0, n-1,0);
auto it = p.begin();
cout << it->second;
while (++it != p.end())
cout << " " << it->second;
return 0;
}
后序中序转层序建树法
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
typedef struct node {
int data;
node* left, *right;
}*Tree;
vector<int>in, post;
Tree pre(int root,int start, int end)
{
if (start > end) return NULL;
int i = start;
Tree T = new node;
T->data = post[root];
while (i < end && in[i] != post[root]) i++;
T->left=pre(root - end + i - 1, start, i - 1);
T->right=pre(root - 1, i + 1, end);
return T;
}
void preprint(Tree T)
{
if (T != NULL) {
cout << T->data;
preprint(T->left);
preprint(T->right);
}
}
void levelorder(Tree T)
{
queue<Tree>q;
int i;
q.push(T);
int num = 0;
while (!q.empty()) {
Tree tmp = q.front();
q.pop();
if (!num)
cout << tmp->data;
else
cout << " " << tmp->data;
num++;
if (tmp->left)
q.push(tmp->left);
if (tmp->right)
q.push(tmp->right);
}
}
int main()
{
Tree T;
int n; cin >> n;
in.resize(n); post.resize(n);
for (int i = 0; i < n; i++)
cin >> post[i];
for (int i = 0; i < n; i++)
cin >> in[i];
T = pre(n - 1, 0, n - 1);
levelorder(T);
}
后序中序转前序
#include<iostream>
#include<vector>
using namespace std;
vector<int>post, in;
void pre(int root, int start, int end)
{
if (start > end)return;
int i = start;
while (i < end && in[i] != post[root])
i++;
cout << post[root] <<ends;
pre(root - 1 - end + i, start, i - 1);
pre(root - 1, i + 1, end);
}
int main()
{
int n; cin >> n;
int i;
for (int i = 0; i < n; i++)
{
int tmp; cin >> tmp;
post.push_back(tmp);
}
for (int i = 0; i < n; i++)
{
int tmp; cin >> tmp;
in.push_back(tmp);
}
pre(5, 0, 5);
}
前序中序转后序
#include<iostream>
#include<vector>
using namespace std;
vector<int>pre, in;
struct node
{
int data;
node* left, * right;
};
typedef node* tree;
tree post(int root,int start,int end)
{
if (start > end)return NULL;
int i = start;
tree T= new node;
T->data = pre[root];
while (i != end && in[i] != pre[root])
i++;
T->left=post(root + 1, start, i - 1);
T->right=post(root + i - start + 1, i + 1, end);
return T;
}
void print(tree T)
{
if (T != NULL) {
print(T->left);
print(T->right);
cout << " "<<T->data;
}
}
int main()
{
int i,n;
scanf_s("%d", &n);
pre.resize(n);
in.resize(n);
for (i = 0; i < n; i++) scanf_s("%d", &pre[i]);
for (i = 0; i < n; i++) scanf_s("%d", &in[i]);
tree T = post(0, 0, n - 1);
print(T);
return 0;
}