"UVA548" Tree achievements + Deep Search (2/1000)

Source: Internet
Author: User
Description

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. Input

The input file would contain a description of the binary tree given as the inorder and postorder traversal sequences of tha T tree. Your program would read the until end of file from the input file. The first line would contain the sequence of values associated with a inorder traversal of the tree and the second line WI ll contain the sequence of values associated with a postorder traversal of the tree. All values would be different, greater than zero and less than 10000. Assume that no binary tree would 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

A very straightforward problem, give you a middle sequence traversal and post-order traversal, ask you to the leaf node path of the least weight of the leaf node's weight, (the path of the weight is the weight of each node plus), if there are more than one path of equal weight, the output leaf node weight of the smallest leaf node weights, good around.

The idea is very intuitive, build a tree, and then deep search, OK, is a water problem.

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <

Sstream> using namespace std;
const int N=10000+10;
    struct node{int left;
    int right;
int weight;
}tree[n];
String str;
int n,x,root,ans,sum=0,tmp=0x3f3f3f3f;


int in[n],post[n];

    int build (int l1,int r1,int l2,int r2) {if (L1&GT;R1) return 0;
    int tmp=post[r2],pos=0;
            for (int i=l1;i<=r1;i++) {if (in[i]==tmp) {pos=i;
        Break
    }} int len=pos-l1;
    Tree[pos].left=build (l1,pos-1,l2,l2+len-1);
    Tree[pos].right=build (pos+1,r1,l2+len,r2-1);
    Tree[pos].weight=in[pos];
return POS;
    } void Dfs (int cur) {sum+=tree[cur].weight; if (tree[cur].left==0&&tree[cur].right==0) {if (sum<tmp) | | | (Sum==tmp&&tree[cur].weight<ans))
            {tmp=sum;
        Ans=tree[cur].weight;
    }} if (Tree[cur].left) DFS (tree[cur].left); if (tree[cur].right) DFS (Tree[cur].right);
Sum-=tree[cur].weight;
    } int main () {//freopen ("In.txt", "R", stdin);
    Freopen ("OUT.txt", "w", stdout);
        while (Getline (CIN,STR)) {tmp=0x3f3f3f3f;
        sum=0;

        memset (tree,0,sizeof (tree));
        N=1;
        StringStream SS (str);
            while (ss>>x) {in[n]=x;
        n++;
        } getline (CIN,STR);
        N=1;
        StringStream SS1 (str);
            while (ss1>>x) {post[n]=x;
        n++;
        } root=build (1,n-1,1,n-1);
        DFS (root); 
    cout<<ans<<endl;
} return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.