这题主要是根据中序遍历和后序变量构造二叉树。
一开始用 的是二叉树的数组表示形式来求解,但是本题的数据量挺大的,数组不能完全表示出来。关键点是这题的所有数都小于10000.所以用两个数组 lch 和rch 来表示权值点的左右孩子的情况。
然后在通过dfs来查找叶子结点到根节点的最小权值的叶子。
代码:
#include <iostream>
#include <cstring>
#include <sstream>
#include <algorithm>
using namespace std;
int in_order[10010], post_order[10010];
int lch[10100], rch[10100];
int n;
bool read_list(int *a)
{
char line[100000];
if(!cin.getline(line, 100000))
return false;
// cout << line << endl;
stringstream ss(line);
n = 0;
int x;
while(ss >> x)
{
a[n++] = x;
// cout << x << ' ';
}
return n > 0;
}
int build(int L1, int R1, int L2, int R2)
{
if(L1 > R1)
return 0;
int root = post_order[R2];
int p = L1;
while(in_order[p] != root) p++;
int num = p - L1;
lch[root] = build(L1, p - 1, L2, L2 + num - 1);
rch[root] = build(p + 1, R1, L2 + num, R2 - 1);
return root;
}
int Min = 0xfffffff, node;
void dfs(int u, int sum)
{
sum += u;
if(!lch[u] && !rch[u])
{
if(sum < Min || (sum == Min && u < node))
{
node = u;
Min = sum;
}
}
if(lch[u])
dfs(lch[u], sum);
if(rch[u])
dfs(rch[u], sum);
}
int main()
{
// freopen("2.txt", "r", stdin);
while(read_list(in_order))
{
read_list(post_order);
build(0, n - 1, 0, n - 1);
Min = 0xfffffff; node = 0;
// for(int u = 1; u <= max; u++)
// cout << tree[u] << ' ';
// cout << endl;
dfs(post_order[n - 1], 0);
cout << node << endl;
}
return 0;
}