UVA 548
来源:互联网 发布:豆腐软件 编辑:程序博客网 时间:2024/06/08 10:25
题目大意:给你二叉树的中序遍历,和后序遍历的结果。让你求,权值最小的叶子节点。也就是从根到叶子结点的值的和最小的点。如果权值相等,输出叶子节点值较小的点。
解题思路:由后序遍历得到根节点的值。即后序遍历最后一个为根,然后在中序遍历中查找这个根的值的位置,左边为左子树,右边为右子树。针对左子树也是同样操作,找“根”(左子树的全体作为树),根的左右子树。也就是递归求树。建树。这是第一题完全自己做得...刚开始以为是求最小叶子节点,后面根据写完的代码,改一下就有了树,所以这里我不会讲思路的。美滋滋。欢迎一起讨论
ac代码:
#include <iostream>#include <map>using namespace std;multimap <int, int>ma;map <int, int>::iterator it;int a[10005], b[10005], count;using namespace std;struct tree{int data;int sum;tree *left;tree *right;tree(){data = sum = 0;left = right = NULL;}}; tree *ro;tree* dfs(int m, int n, int t, int k, tree* ro){int root=b[n];tree *node;node = new tree;node->data = root;node->sum = ro->sum + root;if (k-t == 1){ma.insert(make_pair(node->sum, node->data));return node;}for (int i=t; i<k; i++)if (a[i] == root){node->left = dfs(m, m+i-t-1, t, i, node);node->right = dfs(m+i-t, n-1, i+1, k, node); break;}}int input(int a[]){char c = getchar();int i = 1;while (c != '\n'){scanf("%d", &a[i++]);c = getchar();}return i;}int main(){int i;char c;while (scanf("%d", &a[0])!=EOF){ro = new tree;input(a);scanf("%d", &b[0]);count = input(b);dfs(0, count-1, 0, count, ro);it=ma.begin();cout << it->second << endl;ma.clear();}return 0;}
阅读全文
0 0
- uva 548
- UVA 548
- uva 548
- uva 548
- uva 548
- uva 548
- uva 548
- uva 548
- uva 548
- uva 548
- UVa 548
- uva 548
- UVA 548
- Uva 548
- UVA 548
- UVa 548 Tree
- uva 548 Tree
- uva 548 - Tree
- 网络随笔(七)------XML中DTD,XSD的区别与应用
- hdu6156 Palindrome Function (数位dp)
- win10安装ubuntu双系统出现的问题
- Junit进行数据库操作时事务自动回滚
- Java多线程--守护线程(Daemon)
- UVA 548
- Java Arrays.asList注意事项
- UVA 297
- 【笔记】Python学习笔记 ——1
- PHP加密方法
- CentOS 7 yum 安装 Nginx
- 二、Elastic5.5.2安装中文分词器教程及简单测试
- UVA 712
- 自写过滤器替代ValidateAntiForgeryToken解决asp.net mvc关于提供的防伪标记适用于其他基于声明的用户,而不适用于当前用户的错误