【BUAA 591】The Last Alpha Star
2015-06-20 16:16
260 查看
【BUAA 591】The Last Alpha Star
二叉树 建树后DFS输出符合路径
最主要坑在建树。。。费了些神 还是不够
代码
二叉树 建树后DFS输出符合路径
最主要坑在建树。。。费了些神 还是不够
代码
#include using namespace std; typedef struct Node Node; typedef struct Node *Nd; typedef struct Node { int data; Nd r,l; }Node; int d,tp,f; int num[5555]; bool SetTree(Nd *t) { (*t) = (Nd)malloc(sizeof(Node)); char ch; int x = 0,f = 0; ch = getchar(); while(ch == '\n' || ch == '\r') ch = getchar(); while(ch >= '0' && ch <= '9') { f = 1; x = x*10+ch-'0'; ch = getchar(); } if(f) { (*t)->data = x; } else (*t) = NULL; // cout<<tp<<" "<<x<<endl; if(ch == '(') { tp++; if(SetTree(&((*t)->l))) getchar(); if(SetTree(&((*t)->r))) getchar(); tp--; return 1; } else { if(f) { (*t)->l = NULL; (*t)->r = NULL; } return 0; } } void Dfs(int sum,Nd t) { //cout<<sum<<endl; int i; if(t->l == NULL && t->r == NULL && sum == d) { f = 1; for(i = 0; i < tp;++i) { if(i) printf(" "); printf("%d",num[i]); } printf("\n"); }else if(sum > d) return; if(t->l) { num[tp++] = t->l->data; Dfs(sum+num[tp-1],t->l); tp--; } if(t->r) { num[tp++] = t->r->data; Dfs(sum+num[tp-1],t->r); tp--; } } int main() { //freopen("in.txt","r",stdin); int t,z = 0; scanf("%d",&t); char ch; while(t--) { d = 0; Nd tr; SetTree(&tr); ch = getchar(); tp = 0; while(ch == '\n') ch = getchar(); while(ch != '\n' && ch != '\r') { d = d*10+ch-'0'; ch = getchar(); } tp = f = 0; printf("Case %d:\n",++z); num[tp++] = tr->data; Dfs(num[0],tr); if(!f) printf("I am so sorry!\n"); } return 0; }
相关文章推荐
- Hadoop集群(第4期)_SecureCRT使用
- IO流学习二
- 最小生成树
- android应用的单元测试
- Esper入门简介:二 个人对它的理解
- 毕业课题之----HOG+SVM相关函数的解释
- SQL Server 有关EXCEPT和INTERSECT使用
- Bash的类型(交互、登录)及其相关配置文件
- 第四次作业:《国际贸易学》—关税壁垒措施
- Hadoop集群(第3期)_VSFTP安装配置
- linux程序设计——MySQL管理(第八章)
- WPF学习(二) - 绑定
- 统计学习方法——CART, Bagging, Random Forest, Boosting
- 安装memcached扩展 验证过了可行
- Autolayout
- POJ 1655 Balancing Act 树的重心 基础题
- 图书管理系统——测试与调试
- scala进阶:Future的理解及使用
- Hive文件存储格式的测试比较
- 我眼中的 Docker(二)Image