UVA548
来源:互联网 发布:时时彩软件平刷王 编辑:程序博客网 时间:2024/05/17 01:34
大意:给定前序和后序遍历,先找出从根到叶sum最小的路径,然后输出叶子上的值,如果有多条路径满足,输出值最小的。
紫书155页
代码:#include <bits/stdc++.h>using namespace std;#define LL long long#define INF 0x3f3f3f3#define pi acos(-1)const int maxn=1e3+5;const int maxx=1e6+5;struct node{ struct node *left; struct node *right; int v;};node *root;char s[100005];int v1[100005],v2[100005],top;int init(char *s,int *v){ int top=0; for(int i=0;s[i];i++) { while(s[i]==' ') i++; v[top]=0; while(s[i]&&isdigit(s[i])) { v[top]=v[top]*10+s[i]-'0'; i++; } top++; if(!s[i]) break; } return top;}int find(int *v,int n,int c){ for(int i=n-1;i>=0;i--) if(v[i]==c) return i; return 0;}node *build(int n,int *v1,int *v2){ if(n<=0) return NULL; int p=find(v1,n,v2[n-1]); node *root=(node *)malloc(sizeof(node));//申请内存 也可以用new node() root->v=v2[n-1];//根结点 root->left=build(p,v1,v2); root->right=build(n-p-1,v1+p+1,v2+p); return root;}int min_sum,ans;void dfs(node *root,int sum){ if(root==NULL) return ; sum+=root->v; if(root->left==NULL&&root->right==NULL) { if(min_sum>sum) { min_sum=sum; ans=root->v; } else if(min_sum==sum) ans=min(ans,root->v); return; } dfs(root->left,sum); dfs(root->right,sum);}int main(){ while(gets(s)) { int v; init(s,v1); gets(s); top=init(s,v2); root=build(top,v1,v2); ans=min_sum=INF; dfs(root,0); printf("%d\n",ans); } return 0;}
0 0
- UVA548
- UVA548
- uva548
- uva548
- Uva548
- UVA548
- UVA548
- UVA548
- UVA548
- uva548 Tree
- UVA548 - Tree
- UVA548- Tree
- Tree UVA548
- uva548 - Tree
- tree uva548
- uva548 Tree
- uva548 -- Tree
- uva548(树)
- 关于SSM整合中遇到的org.springframework.beans.factory.BeanDefinitionStoreException: IOException pars 问题
- Zigbee绑定流程
- Leetcode Generate Parentheses 22
- PHP函数补完:call_user_func()
- 第五章 格式
- UVA548
- Linux--多网卡的7种Bond模式
- 经典面试题之 —— 成员变量和局部变量的区别
- 统计学习精要 (Elements of Statistical Learning ) 习题 2.3
- PHP Notice: undefined index 完美解决方法
- 第六章 对象和数据结构
- JSP内置对象(一)
- Matlab GUI回调函数规则
- Java的clone方法