UVa 548 Tree(建树,递归遍历)

题意  给你一个树的中序遍历和后序遍历  某个节点的权值为从根节点到该节点所经过节点的和  求权值最小的叶节点的值  如果存在多个  输出值最小的那个

把树建好就好说了  递归递归dfs msun保存最小叶节点权值  ans保存答案

#include<cstdio>
#include<cctype>
#include<cstring>
using namespace std;
const int maxn = 10005, INF = 0x3f3f3f3f;
int in[maxn], post[maxn], msum, ans;

struct node
{
    int val;
    node *left, *right;
    node(): left(NULL), right(NULL) {}
};

node* build(int le, int ri, int pos)
{
    node *cur = new node;
    cur->val = post[pos];
    if(le == ri) return cur;
    int i = le;
    while(in[i] != post[pos]) ++i;
    if(i == le) cur->left = NULL;
    else  cur->left = build(le, i - 1, pos - 1 - (ri - i));
    if(i == ri) cur->right = NULL;
    else cur->right = build(i + 1, ri, pos - 1);
    return cur;
}

void dfs(node *cur, int sum)
{
    int t = cur->val;
    if(!(cur->left || cur->right))
    {
        if(sum + t < msum || (sum + t == msum && t < ans))
            msum = sum + t, ans = t;
    }
    if(cur->left != NULL) dfs(cur->left, sum + t);
    if(cur->right != NULL) dfs(cur->right, sum + t);

}

int main()
{
    char s[maxn*10];
    while(gets(s) != NULL)
    {
        int t = 0, l = 0;
        for(int i = 0; s[i] != '\0'; ++i)
            if(isdigit(s[i])) t = t * 10 + s[i] - '0';
            else  in[++l] = t, t = 0;
        in[++l] = t;

        gets(s);
        l = t = 0;
        for(int i = 0; s[i] != '\0'; ++i)
            if(isdigit(s[i])) t = t * 10 + s[i] - '0';
            else post[++l] = t, t = 0;
        post[++l] = t;

        node *root = build(1, l, l);
        ans = msum = INF;
        dfs(root, 0);
        printf("%d\n", ans);
    }
    return 0;
}

 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

 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
时间: 2024-12-29 19:26:54

UVa 548 Tree(建树,递归遍历)的相关文章

UVa 548 (二叉树的递归遍历) Tree

题意: 给出一棵由中序遍历和后序遍历确定的点带权的二叉树.然后找出一个根节点到叶子节点权值之和最小(如果相等选叶子节点权值最小的),输出最佳方案的叶子节点的权值. 二叉树有三种递归的遍历方式: 先序遍历,先父节点  然后左孩子  最后右孩子 中序遍历,先左孩子  然后父节点  最后父节点 后序遍历,先左孩子  然后右孩子  最后父节点 这里有更详细的解释: http://blog.csdn.net/sicofield/article/details/9066987 紫书上面写错了,后序遍历最后一

[2016-02-08][UVA][548][Tree]

UVA - 548 Tree Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description 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

UVa 548 Tree【二叉树的递归遍历】

题意:给出一颗点带权的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小. 学习的紫书:先将这一棵二叉树建立出来,然后搜索一次找出这样的叶子结点 虽然紫书的思路很清晰= =可是理解起来好困难啊啊啊啊 后来终于问懂一丢丢了--- 比如说样例: 中序遍历:3 2 1 4 5 7 6 后序遍历:3 1 2 5 6 7 4 首先做第一层: 在后序遍历中的最后一个数为根节点,然后在到中序遍历中找到这个根节点,在这个根节点的左边是左子树,右边是右子树,这样就确定出了左子树和右子树的区间 然后做第

UVA - 548 Tree(二叉树的递归遍历)

题意:已知中序后序序列,求一个叶子到根路径上权和最小,如果多解,则叶子权值尽量小. 分析:已知中序后序建树,再dfs求从根到各叶子的权和比较大小 #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<itera

UVa 548 Tree (建树+前序后序)

Description You are to determine the value of the leaf node in a given binary tree that is the terminal node of apath of least value from the root of the binary tree to any leaf. The value of a path is the sum of valuesof nodes along that path.InputT

Uva 548 Tree

0.这是一道利用中序遍历和后序遍历确定二叉树的题目,学会建树 关键点理解这段代码 int build(int L1,int R1,int L2,int R2) { //printf("built:\n"); if(L1>R1) return 0;//空树 int root=post_order[R2]; int p=L1; while(in_order[p] != root) p++; int cnt = p-L1;//左子树的结点个数 lch[root]=build(L1,p-

Tree UVA - 548 已知中序遍历和后序遍历,求这颗二叉树。

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

UVA 548(二叉树重建与遍历)

J - Tree Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Appoint description:  System Crawler  (2014-05-16) Description  Tree  You are to determine the value of the leaf node in a given binary tree that is the ter

UVA 699(二叉树建树与遍历)

M - The Falling Leaves Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Appoint description:  System Crawler  (2014-02-08) Description  The Falling Leaves  Each year, fall in the North Central region is accompanied