zoukankan      html  css  js  c++  java
  • 树 (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;
        }
    }
  • 相关阅读:
    winForm ComboBox 控件默认值绑定及只可选择不可输入设定处理
    [c#]CacheHelper缓存类
    access数据库用sql语句添加字段,修改字段,删除字段
    35岁前程序员要规划好的四件事
    C#将网页内容转换成图片保存到本地( webbrowser 可应用于B/S结构中)
    SQL中返回刚插入记录的ID
    JIRA破解
    C#数组查找与排序
    最好的缺陷管理软件下载及破解Jira3.10 Enterprise Edition
    sql2000数据库 sql语句C#分页类代码
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6738124.html
Copyright © 2011-2022 走看看