Fork me on GitHub

Uva 548 - Tree

  Tree 

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

 

 


Miguel A. Revilla 
1999-01-11
 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define MAXN 10100
typedef struct BiTree{
    int value;
    struct BiTree *lch, *rch;
}BiTree, *BiNTree;

int ans = 0;

void getch(int *inter, char ch, int& flag, int& cnt)
{//处理输入的函数 
    if(isdigit(ch))
        {
            inter[cnt] = inter[cnt]*10 + (ch-'0');
            flag = 1;
        }
        else if(flag) 
        {
            flag = 0;
            cnt++;
        }
    return;
}

void Traverse(BiNTree& tree, int low, int high, int *inter, int *post, int front, int rear)
{//根据中序和后序建树(基础) 
    int i = 0;
    int value = post[rear];
    tree = (BiNTree)malloc(sizeof(BiTree));
    tree->value = value, tree->lch = NULL, tree->rch = NULL;
    for(i=low; i<=high && inter[i] != value; ++i) ;
    
    if(i > low)
    {
        Traverse(tree->lch, low, i-1, inter, post, front, front+(i-low-1));
    }
    if(i < high)
    {
        Traverse(tree->rch, i+1, high, inter, post, front+i-low, rear-1);
    }
    return;
}

void search(BiNTree& tree, int& max, int sum)
{//遍历二叉树,找到满足要求的值(即从树根到叶子节点路上所有节点之和最小的所在路的叶子节点的值) 
    sum += tree->value;
    if(tree->rch == NULL && tree->lch == NULL && max > sum)
    ans = tree->value, max = sum;

    if(tree->lch != NULL)
        search(tree->lch, max, sum);
    if(tree->rch != NULL)
        search(tree->rch, max, sum);
    free(tree);
    return;
}

int main()
{

    int inter[MAXN], post[MAXN];
    char ch;
    while((ch = getchar()) != EOF)
    {
        int cnt = 0, flag = 0;
        memset(inter, 0, sizeof(inter));
        getch(inter,ch, flag, cnt);
        while((ch = getchar()) != '\n')
            getch(inter, ch, flag, cnt);
            
        if(flag) 
        {
            flag = 0;
            cnt++;
        }
         
        for(int i=0; i<cnt; ++i)
        scanf("%d", &post[i]); //输入后序数组 
        getchar();
        BiNTree tree = NULL;
        tree = (BiNTree)malloc(sizeof(BiTree));
        Traverse(tree, 0, cnt-1, inter, post, 0, cnt-1);
        int sum = 0;
        cnt = 0;
        int max = 100000000;
        search(tree, max, cnt);
        printf("%d\n", ans);
    }
    return 0;
}

解题思路:

思路比较简单,开始根据中序和后序遍历建立二叉树,建立完二叉树后找到需要的值

CE: 提交时忘了选择为C++

RE:处理输入出了问题,错误地用了fgets函数将整行的输入数据存储到输入当中,后来才发现最小数组的长度为10000*10000!

posted @ 2013-03-24 18:52  Gifur  阅读(289)  评论(0编辑  收藏  举报
TOP