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;
        }
    }
  • 相关阅读:
    品优购(IDEA版)-第二天
    品优购(IDEA版)-第一天
    GitHub客户端Desktop的安装和使用总结
    Android开发10:传感器器及地图相关应用
    Android开发9:网络访问&Web服务开发
    Android开发8:数据存储(二)——SQLite数据库和ContentProvider的使用
    Android开发7:简单的数据存储(使用SharedPreferences)和文件操作
    Android开发6:Service的使用(简单音乐播放器的实现)
    归纳整理一些工作学习中发现的不错的网站、博客地址等(转载)
    Android Studio安装配置、环境搭建详细步骤及基本使用
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6738124.html
Copyright © 2011-2022 走看看