uva 558 tree(不忍吐槽的题目名)——yhx

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.

 1 #include<cstdio>
 2 #include<cstring>
 3 struct node
 4 {
 5     int lch,rch;
 6 }a[10010];
 7 int zx[10010],hx[10010],n,ans=0x7f7f7f7f,ans_leaf=0x7f7f7f7f;
 8 bool rdzx()
 9 {
10     int i,j,k,p,q,x,y,z;
11     char c;
12     if (scanf("%d",&zx[1])==-1) return 0;
13     n=1;
14     while (scanf("%c",&c)&&c==‘ ‘)
15       scanf("%d",&zx[++n]);
16     return 1;
17 }
18 void rdhx()
19 {
20     int i;
21     for (i=1;i<=n;i++)
22       scanf("%d",&hx[i]);
23 }
24 int crt(int lz,int rz,int lh,int rh)
25 {
26     if (lz>rz) return 0;    //注意终止条件
27     int i,j,k,p,q,x,y,z;
28     p=hx[rh];
29     i=lz;
30     while (zx[i]!=p) i++;
31     a[p].lch=crt(lz,i-1,lh,i+lh-lz-1);   //关键在于这两个式子的推导,列方程求比较不费脑子
32     a[p].rch=crt(i+1,rz,rh-rz+i,rh-1);
33     return p;
34 }
35 void sc(int p,int x)
36 {
37     int i,j,k,l,m,y,z;
38     x+=p;
39     if (!a[p].lch&&!a[p].rch)
40     {
41         if (x<ans||(x==ans&&p<ans_leaf))
42         {
43             ans=x;
44             ans_leaf=p;
45             return;
46         }
47     }
48     if (a[p].lch) sc(a[p].lch,x);
49     if (a[p].rch) sc(a[p].rch,x);
50 }
51 int main()
52 {
53     int i,j,k,l,m,p,q,x,y,z;
54     while (rdzx())
55     {
56         rdhx();
57         memset(a,0,sizeof(a));
58         crt(1,n,1,n);
59         ans=ans_leaf=0x7f7f7f7f;
60         sc(hx[n],0);
61         printf("%d\n",ans_leaf);
62     }
63 }

递归求树,由于后序遍历的最后一个元素一定是根,所以可以在中序中找到根的位置,则其左边为左子树,右边为右子树,分治求解。

求值相对简单,dfs即可。

时间: 2024-10-13 12:02:40

uva 558 tree(不忍吐槽的题目名)——yhx的相关文章

[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 558 Wormholes (Bellman-Ford算法判断负环)

uva 558 Wormholes In the year 2163, wormholes were discovered. A wormhole is a subspace tunnel through space and time connecting two star systems. Wormholes have a few peculiar properties: Wormholes are one-way only. The time it takes to travel throu

uva 10410 - Tree Reconstruction(栈)

题目链接:uva 10410 - Tree Reconstruction 题目大意:给定一个树的BFS和DFS,求这棵树. 解题思路:用栈维护即可.对应BFS序列映射出了每个节点和根节点的距离,遍历dfs序列,对当前节点和栈顶节点比较,如果该节点距离根节点更远,则说明该节点为栈顶节点个孩子节点,则记录后将节点放入栈中.否则弹掉栈顶元素继续比较.需要注意一点,即当元素与栈顶元素的距离值大1的时候要视为相等,因为它们属于兄弟节点 #include <cstdio> #include <cst

UVA 10410 - Tree Reconstruction(树)

UVA 10410 - Tree Reconstruction 题目链接 题意:给定一个树的dfs序列和bfs序列,求出这颗树 思路:拿dfs的序列,分成若干段,每一段相当一个子树,这样就可以利用bfs的序列去将dfs的序列分段,然后利用一个队列去存放每一段,不断求出子树即可.一开始以为parse tree一定是二叉树,原来不一定啊. 代码: #include <cstdio> #include <cstring> #include <vector> #include

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

uva 112 - Tree Summing

 Tree Summing  Background LISP was one of the earliest high-level programming languages and, with FORTRAN, is one of the oldest languages currently being used. Lists, which are the fundamental data structures in LISP, can easily be adapted to represe

UVa 112 - Tree Summing(树的各路径求和,递归)

题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=48 Tree Summing  Background LISP was one of the earliest high-level programming languages and, with FORTRAN, is one of the olde

UVA 558 Wormholes 【SPFA 判负环】

题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=499 题意:就是判断图中有无负环 SPFA,某个节点入队次数大于n就是有负环. 代码: #include <iostream> #include <stdio.h> #include <string.h> #include <algori

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