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-1,L2,L2+cnt-1);
    rch[root]=build(p+1,R1,L2+cnt,R2-1);
    return root;
}

1.剩下的就是递归了 注意一下递归边界是  到达叶子结点 即左右子树均为空的结点 就行了

 if(!lch[v] && !rch[v])
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <sstream>
 5 #include <algorithm>
 6 using namespace std;
 7 const int maxn=10000 + 5;
 8 int post_order[maxn],in_order[maxn],lch[maxn],rch[maxn];
 9 int n,ans_sum,ans_v;
10
11 bool input(int*a)
12 {
13     string s;
14     //getline(cin,s);
15     if(!getline(cin,s)) return false;
16     stringstream ss(s);
17     n=0;
18     int x;
19     while(ss>>x) a[n++]=x;
20     return n>0;
21 }
22
23 int build(int L1,int R1,int L2,int R2)
24 {
25     //printf("built:\n");
26     if(L1>R1) return 0;//空树
27     int root=post_order[R2];
28     int p=L1;
29     while(in_order[p] != root) p++;
30     int  cnt = p-L1;//左子树的结点个数
31     lch[root]=build(L1,p-1,L2,L2+cnt-1);
32     rch[root]=build(p+1,R1,L2+cnt,R2-1);
33     return root;
34 }
35 void dfs(int v,int sum)
36 {
37     sum+=v;
38     if(!lch[v] && !rch[v])
39     {
40         if(sum < ans_sum)
41             ans_sum = sum,ans_v=v;
42         else if(sum == ans_sum && v < ans_v)
43             ans_v = v;
44     }
45     if(lch[v]) dfs(lch[v],sum);
46     if(rch[v]) dfs(rch[v],sum);
47 }
48 int  main()
49 {
50     while(input(in_order))
51     {
52         input(post_order);
53         build(0,n-1,0,n-1);
54         ans_sum=999999999;
55         dfs(post_order[n-1],0);
56         printf("%d\n",ans_v);
57     }
58     return 0;
59 }
时间: 2025-01-01 14:36:43

Uva 548 Tree的相关文章

[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(建树,递归遍历)

题意  给你一个树的中序遍历和后序遍历  某个节点的权值为从根节点到该节点所经过节点的和  求权值最小的叶节点的值  如果存在多个  输出值最小的那个 把树建好就好说了  递归递归dfs msun保存最小叶节点权值  ans保存答案 #include<cstdio> #include<cctype> #include<cstring> using namespace std; const int maxn = 10005, INF = 0x3f3f3f3f; int i

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【二叉树的递归遍历】

题意:给出一颗点带权的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小. 学习的紫书:先将这一棵二叉树建立出来,然后搜索一次找出这样的叶子结点 虽然紫书的思路很清晰= =可是理解起来好困难啊啊啊啊 后来终于问懂一丢丢了--- 比如说样例: 中序遍历: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

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 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)

Tree UVA - 548 题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的.还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的. 最后输出来叶子节点. 一开始写的时候是用gets读入的,报CE, 要用fgets写,关于fgets(),传送门: fgets函数及其用法,C语言fgets函数详解 一开始用bfs过的,后来发现,好多人都是dfs过的,又写了一下dfs... 代码: 1 //二叉树的中序和后序遍历还原树并输出最短路径的叶子

UVA - 112 - Tree Summing (数的求和!栈的应用!)

UVA - 112 Tree Summing Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Tree Summing  Background LISP was one of the earliest high-level programming languages and, with FORTRAN, is one of the oldest