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 @   Gifur  阅读(289)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
TOP
点击右上角即可分享
微信分享提示