树 (p155, 从中序和后续回复二叉树)

递归求解,

You are to determine the value of the leaf node in a given binary tree that is the terminal node of a path of least value from the root of the binary tree to any leaf. The value of a path is the sum of values of nodes along that path.

Input The input file will contain a description of the binary tree given as the inorder and postorder traversal sequences of that tree. Your program will read two line (until end of file) from the input file. The first line will contain the sequence of values associated with an inorder traversal of the tree and the second line will contain the sequence of values associated with a postorder traversal of the tree.

All values will be different, greater than zero and less than 10000. You may assume that no binary tree will have more than 10000 nodes or less than 1 node. Output For each tree description you should output the value of the leaf node of a path of least value.

Inthe case of multiple paths of least value you should pick the one with the least value on the terminal node.

Sample Input

3 2 1 4 5 7 6

3 1 2 5 6 7 4

7 8 11 3 5 16 12 18

8 3 11 7 16 18 12 5

255

255

Sample Output 1 3 255

#include<iostream>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN  10003
#define INF 1000000009
int a[MAXN], b[MAXN];//中序遍历 和 后序遍历
int ans,k;
struct node
{
    int left, right;
}T[MAXN];
int build(int inbeg, int inend, int pbeg, int pend)
{
    if (inbeg > inend || pbeg > pend)
        return -1;
    int r = b[pend];
    int p = inbeg,cnt = 0;
    while (a[p] != r)
        p++;
    cnt = p - inbeg;
    T[r].left = build(inbeg, p - 1, pbeg, pbeg + cnt-1);
    T[r].right = build(p + 1, inend, pbeg + cnt, pend - 1);
    return r;
}
void get_ans(int x, int sum)
{
    sum += x;
    if (T[x].left == -1 && T[x].right == -1)
    {
        if (sum < ans || (sum == ans&&x < k))
        {
            ans = sum;
            k = x;
        }
    }
    if (T[x].left != -1) get_ans(T[x].left, sum);
    if (T[x].right != -1) get_ans(T[x].right, sum);
}
int main()
{
    string tmp;
    while (getline(cin, tmp))
    {
        stringstream s(tmp);
        int i = 0, j = 0;
        while (s >> a[i]) i++;
        getline(cin, tmp);
        stringstream s1(tmp);
        for (j = 0; j < i; j++)
        {
            s1 >> b[j];
        }
        int n = i, root = b[i - 1];
        ans = INF;
        for (int i = 1; i < MAXN; i++)
            T[i].left = T[i].right = 0;
        build(0, n - 1, 0, n - 1);
        get_ans(root, 0);
        cout << k << endl;
    }
}

 把求解和递归合并

#include<iostream>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN  10003
#define INF 1000000009
int a[MAXN], b[MAXN];//中序遍历 和 后序遍历
int ans,k;
struct node
{
    int left, right;
}T[MAXN];
int build(int inbeg, int inend, int pbeg, int pend,int sum)
{
    if (inbeg > inend || pbeg > pend)
        return -1;
    int r = b[pend];
    sum += r;
    int p = inbeg,cnt = 0;
    while (a[p] != r)
        p++;
    cnt = p - inbeg;
    T[r].left = build(inbeg, p - 1, pbeg, pbeg + cnt-1,sum);
    T[r].right = build(p + 1, inend, pbeg + cnt, pend - 1,sum);
    if (T[r].left == -1 && T[r].right == -1)
    {
        if (sum < ans || (sum == ans&&r < k))
        {
            ans = sum;
            k = r;
        }
    }
    return r;
}
/*
void get_ans(int x, int sum)
{
    sum += x;
    if (T[x].left == -1 && T[x].right == -1)
    {
        if (sum < ans || (sum == ans&&x < k))
        {
            ans = sum;
            k = x;
        }
    }
    if (T[x].left != -1) get_ans(T[x].left, sum);
    if (T[x].right != -1) get_ans(T[x].right, sum);
}
*/
int main()
{
    string tmp;
    while (getline(cin, tmp))
    {
        stringstream s(tmp);
        int i = 0, j = 0;
        while (s >> a[i]) i++;
        getline(cin, tmp);
        stringstream s1(tmp);
        for (j = 0; j < i; j++)
        {
            s1 >> b[j];
        }
        int n = i, root = b[i - 1];
        ans = INF;
        for (int i = 1; i < MAXN; i++)
            T[i].left = T[i].right = 0;
        build(0, n - 1, 0, n - 1,0);
        cout << k << endl;
    }
}
原文地址:https://www.cnblogs.com/joeylee97/p/6738124.html