UVa 548 tree: Central sequence traversal & sequential traversal &dfs

Source: Internet
Author: User

548-tree

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_ problem&problem=489

You are are to determine the value of the ' leaf node in a given binary ' 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 would contain a description of the binary tree given as the inorder and postorder traversal sequences of tha T tree. Your program would read two line (until end of file) from the input file. The the ' the ' the ' the ' contain the ' sequence of values associated with a inorder traversal the ' tree ' and ' second line WI ll contain the sequence of values associated with a postorder traversal to the tree. All values would be different, greater than zero and less than 10000. You may assume this no binary tree would have more than 10000 nodes or less than 1 node.

Output

For the all 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 for 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 one 3
5
255
8

Sample Output

1
3
255

Combine the achievements with the DFS two steps, as detailed in the code.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Complete code:

/*0.086s*/#include <cstdio> #include <algorithm> using namespace std;  
    
int in[10010], post[10010], min_sum, ans;  
    void Dfs (int n, int* in, int* post, int sum) {if (n <= 0) return;  
    int p = Find (in, in + N, post[n-1])-in;  
    Sum + + post[n-1];  
            if (n = = 1) {if (sum < min_sum) {min_sum = sum;  
        ans = post[n-1];  
            
        ///you should output the value of the leaf node of a path of least value.  
        else if (sum = = min_sum) ans = min (ans, post[n-1]);   
        In the case of multiple paths of least value for you should pick the ' one with the ' least value on the terminal node.  
    Return  
    DFS (P, in, post, sum);///Zoozi Tree Dfs (n-p-1, in + P + 1, post + P, sum);///right subtree int main () {  
    Char ch;  
    int n, I; while (~SCANF ("%d%c", &in[0], &ch) {if (ch = 10///Read Only one number (on line) {scanf ("%d", &post[0]);  
            printf ("%d\n", post[0]);  
        Continue  
        } n = 1;  
        while (scanf ("%d%c", &in[n++], &ch), ch!= 10);  
        for (i = 0; i < n; ++i) scanf ("%d", &post[i]);  
        Ans = min_sum = -1u >> 1;  
        DFS (n, in, post, 0);  
    printf ("%d\n", ans);  
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.