UVa 112 - Tree Summing
2013-05-01 21:43
381 查看
//利用二叉树的递归定义进行递归访问, AC:0.069s #include <iostream> #include <cstdio> #include <cstring> #include <cctype> using namespace std; int found; //是否找到 int sum; //记录树根到当前节点的路径总和 int n; //需要判断的值 //跳过下一个非空字符 void get_next() { char ch; while(true) { ch = getchar(); if(isspace(ch)) continue; //跳过space,\n return; } } // return 0:空树, 1:非空树 int get_tree() { while(true) { get_next(); // ( int val; if(scanf("%d", &val) != 1) {get_next(); return 0;} //空树 sum += val; int left = get_tree(); //遍历左子树 int right = get_tree(); //遍历右子树 if(!left && !right && sum==n) {found = true;} //是叶子节点 且满足条件 get_next(); // ) sum -= val; // 深度减小 return 1; } } int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); #endif while(scanf("%d", &n) == 1) { sum = 0; found = false; get_tree(); if(found) printf("yes\n"); else printf("no\n"); } }
相关文章推荐
- uva112 Tree Summing(二叉树 + 栈)
- UVa 112 - Tree Summing
- UVA 112 Tree Summing
- uva 112 tree summing
- uva 112 - Tree Summing
- UVa 112 - Tree Summing
- UVA 112 Tree Summing
- poj 1145 (uva 112) Tree Summing
- POJ 题目1145/UVA题目112 Tree Summing(二叉树遍历)
- UVA - 112 Tree Summing
- uva 112 Tree Summing
- UVa 112|POJ 1145|Tree Summing|字符串处理|树的遍历
- UVA 112 Tree Summing
- UVA 112 - Tree Summing(栈)
- POJ 题目1145/UVA题目112 Tree Summing(二叉树遍历)
- UVA - 112 Tree Summing
- UVA 112-Tree Summing
- uva 112 Tree Summing
- UVA 112(Tree Summing)回溯
- uva112-Tree Summing[动态建树]