• Tree UVA


    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. In the
    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
    /**
    题目:Tree UVA - 548
    链接:https://vjudge.net/problem/UVA-548
    题意:算法竞赛入门经典P155 eg6-8
    思路:后序遍历的最后一个为根。那么知道了根是多少,就可以在中序遍历找到根的位置,
    根的左边为左子树,右边为右子树。左子树,右子树的后序遍历也可以通过原来的后序遍历中分成两部分获得。
    后序遍历和中序遍历长度相同。
    递归处理每一个结点即可。
    
    收获:
    char s[];
    stringstream ss(s);
    n = 0;
    int x;
    while(ss>>x) a[n++] = x;
    
    
    处理一行由数字和空格组成的字符串,划分成数字的方式。
    */
    
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    typedef pair<int,int> P;
    const int maxn = 1e4+100;
    const int mod = 1e9+7;
    int a[maxn], b[maxn], n;
    struct node
    {
        int value;
        node *left, *right;
        node():left(NULL),right(NULL){}
    };
    node *root;
    char s[10*maxn];
    void Input(char *s,int a[])
    {
        stringstream ss(s);
        n = 0;
        int x;
        while(ss>>x) a[n++] = x;
    }
    node* build(int l,int r,int p)
    {
        if(l>r){
            return NULL;
        }
        node *x = new node();
        int mid;
        for(int i = l; i <= r; i++){
            if(a[i]==b[p]){
                mid = i; break;
            }
        }
        x->value = a[mid];
        x->left = build(l,mid-1,p-1-(r-mid));///可推算出根的位置。
        x->right = build(mid+1,r,p-1);
        return x;
    }
    int ans, leaf;
    void Find(node *root,int sum)
    {
        if(root->left==NULL&&root->right==NULL){
            if(sum+root->value<ans){
                ans = sum + root->value;
                leaf = root->value;
            }else
            {
                if(sum+root->value==ans&&root->value<leaf){
                    leaf = root->value;
                }
            }
        }else
        {
            if(root->left!=NULL){
                Find(root->left,sum+root->value);
            }
            if(root->right!=NULL){
                Find(root->right,sum+root->value);
            }
        }
    }
    int main()
    {
        while(gets(s)!=NULL){
            Input(s,a);
            gets(s);
            Input(s,b);
            root = build(0,n-1,n-1);///中序遍历[0,n-1],第三个n-1表示根。
            ans = maxn*maxn;
            Find(root,0);
            printf("%d
    ",leaf);
        }
        return 0;
    }
  • 相关阅读:
    js对象的深度克隆
    通用事件监听函数
    JavaScript封装
    Ajax
    MarkDown怎么在博客园展现出来
    MarkDown语法学习
    暑期培训第三周SDN总结
    暑期培训遇到的floodlight+mininet+ubuntu的问题
    Ubuntu14.04搭建mininet与可视化工具miniedit介绍
    Ubuntu 14.04 安装 Sublime Text 3,并用Sublime Text 3 安装Package Control
  • 原文地址:https://www.cnblogs.com/xiaochaoqun/p/6882806.html
Copyright © 2020-2023  润新知