UVA548——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

题目大意:

    输入一个二叉树的中序和后序,输出一个叶子节点,该叶子节点到根的数值总和最小。

结题思路:

    先通过后序和中序建立二叉树,在通过DFS进行搜索,找到符合题目要求的叶子。(需要使用全局变量来记录DFS过程中的最小和叶子)

    中序和后序建立二叉树:

      使用递归来逐步建立,由后序连确定当前递归中的分支的根节点,再在中序中找到根的位置,则中序中根左的为左子树的中序排列,根右的为右子树的中序。设此时左子树的长度为len,则当前的后序的前len个数据是左子树的后序排列。同理进行递归即可。右子树同理。

    DFS:

      设一个变量m,每次递归时作为实参进入调用,并执行m+=tree.data,则能保证递归到叶子节点时,m保存的是当前叶子到根节点的和,根据m的大小,即可选出符合题意的叶子节点。

Code:

 1 #include<malloc.h>
 2 #include<iostream>
 3 #include<stdio.h>
 4 #include<string>
 5 #include<cstring>
 6 using namespace std;
 7 struct tree
 8 {
 9     int data;
10     int left;
11     int right;
12 } T[100100]; //数组模拟的二叉树
13 int m_sum=100000000,pos; //用于DFS时保存结果
14 int mid[100100],last[100100];
15 int create_tree(int m1,int m2,int l1,int l2)
16 {
17     if (m1==m2)
18     {
19         T[m1].left=T[m1].right=-1;
20         T[m1].data=mid[m1];
21         return m1;
22     }
23     if (m1>m2)
24         return -1;
25     int i;
26     for (i=0; i<=m2; i++)
27         if (mid[i]==last[l2]) break;
28     T[i].left=create_tree(m1,i-1,l1,l1+i-m1-1);//递归建树!!注意四个参数,runtime好多遍
29     T[i].right=create_tree(i+1,m2,l1+i-m1,l2-1);
30     T[i].data=mid[i];
31     return i;
32 }
33 int min(int a,int b)
34 {
35     return a>b?b:a;
36 }
37 int dfs(int head,int m)
38 {
39     m+=T[head].data;
40     if (T[head].left==-1&&T[head].right==-1)
41     {
42         if (m<m_sum) //打擂台,选出最小的结果,并将叶子节点的数值存入pos中
43         {
44             m_sum=m;
45             pos=T[head].data;
46         }
47         return T[head].data;
48     }
49     int sum=T[head].data;
50     if (T[head].left!=-1&&T[head].right==-1) sum+=dfs(T[head].left,m);
51     else if (T[head].right!=-1&&T[head].left==-1) sum+=dfs(T[head].right,m);
52     else sum+=min(dfs(T[head].left,m),dfs(T[head].right,m));
53     return sum;
54 }
55 int main()
56 {
57     int k1=0,k2=0;
58     while (scanf("%d",&mid[0])!=EOF)
59     {
60         pos=0,m_sum=100000000;
61         k1=1;
62         for (int i=0; i<=10000; i++)
63             T[i].left=T[i].right=-1,T[i].data=0;
64         while (1)
65         {
66             char ch=getchar();
67             if (ch==‘\n‘) break;
68             scanf("%d",&mid[k1++]);
69         }
70         k1--,k2=0;
71         while (1)
72         {
73             scanf("%d",&last[k2++]);
74             char ch=getchar();
75             if (ch==‘\n‘) break;
76         }
77         k2--;
78         int T_head=create_tree(0,k1,0,k2);
79         dfs(T_head,0);
80         printf("%d\n",pos);
81     }
82     return 0;
83 }

UVA548——Tree

时间: 2024-08-08 01:11:21

UVA548——Tree的相关文章

UVA548 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

UVA548 Tree

从这个题目我们再次学习到了一个知识点,就是递归函数的强大! 首先,我们先明确一个知识点,就是你知道了一棵树的中序和后序遍历,求他的前序遍历,我们应该怎么来做? 第一步:最初的时候,我们的后序遍历的最后一个数字就是我们的一个子树的根节点 第二步:找到我们的根结点后,跟据我们中序遍历的性质,我们的树就会被自然地分成了左右两个部分. 第三步:统计出来左右两个子树的大小和长度,这样就能继续重复上面的步骤 这道题的读写输入也是一个知识点,这个我是直接看的刘汝佳的知识点来做的. 总之,递归很强大很强大! #

UVa548 Tree (二叉树)

链接:http://acm.hust.edu.cn/vjudge/problem/19105分析:由中序遍历和后序遍历可以唯一确定一棵二叉树.后序遍历中最后一个元素就是树根,然后在中序遍历中找到它,从而可以找出以它为根结点的左右子树的结点列表,然后再递归构造左右子树.这道题由于权值各不相同,且都是小于10000的正整数,可以以权值作为结点编号建树.边建树边统计最优解,best为最优解叶子结点权值也即叶子结点编号,best_sum为最优权值和,这两个变量作为全局变量,因为每个可行解都要与当前最优情

【日常学习】【二叉树遍历】

这道题目本身不难,给出后序遍历和中序遍历,求到节点最小路径的叶子,相同长度就输出权值小的叶子. Uva上不去了,没法测.基本上是按照ruka的代码来的.直接上代码 //Uva548 Tree #include<iostream> #include<algorithm> #include<string> #include<sstream> #include<cctype> using namespace std; const int maxv=10

二叉树的递归遍历 Tree UVa548

题意:给一棵点带权的二叉树的中序和后序遍历,找一个叶子使得他到根的路径上的权值的和最小,如果多解,那该叶子本身的权值应该最小 解题思路:1.用getline()输入整行字符,然后用stringstream获得字符串中的数字 2.用数组in_oder[]和post_order[]分别表示中序遍历和后序遍历的顺序,用数组rch[]和lch[]分别表示结点的左子树和右子树 3.用递归的办法根据遍历的结果还原二叉树(后序遍历的最后一个树表示的是根节点,中序遍历的中间一个表示根节点) 4.二叉树构造完成后

例题6-8 Tree Uva548

这道题我一直尝试用scanf来进行输入,不过一直没有成功,因此先搁置一下,以后积累些知识再进行尝试. 这道题有两种解决方案: 即先建树,再遍历和边建树边遍历.这两种方案经过实践证实效率相差不太多.应该主要耗时的是cin stringstream 之类的输入函数. 另外,通过这道题领悟了一个非常重要的事情: 一定要清空上组数据使用过的数组,否则后果很严重!!!!!! 除非你确定数组清不清无所谓. 在这里也可以稍微用一些技巧.我在输入的同时将所需要的数组对应下标设为初始值,这样能节省很多时间,特别是

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

easyui js取消选中 Tree 指定节点

取消所有选中 var rootNodes = treeObject.tree('getRoots'); for ( var i = 0; i < rootNodes.length; i++) { var node = treeObject.tree('find', rootNodes[i].id); treeObject.tree('uncheck', node.target); }

Maximum Depth of Binary Tree

这道题为简单题 题目: Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 思路: 我是用递归做的,当然也可以用深搜和广搜,递归的话就是比较左右子树的深度然后返回 代码: 1 # Definition for a binary tre