UVa 112 Tree Summing (scanf()去空格&递归&二叉树遍历)
2013-10-04 12:49
435 查看
112 - Tree Summing
Time limit: 3.000 secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=48
Background
LISP was one of the earliest high-level programming languages and, with FORTRAN, is one of the oldest languages currently being used. Lists, which are the fundamental data structures in LISP, can easily be adapted torepresent other important data structures such as trees.
This problem deals with determining whether binary trees represented as LISP S-expressions possess a certain property.
The Problem
Given a binary tree of integers, you are to write a program that determines whether there exists a root-to-leaf path whose nodes sum to a specified integer. For example, in the tree shown below there are exactly fourroot-to-leaf paths. The sums of the paths are 27, 22, 26, and 18.
Binary trees are represented in the input file as LISP S-expressions having the following form.
empty tree ::= () tree ::= empty tree (integer tree tree)
The tree diagrammed above is represented by the expression (5 (4 (11 (7 () ()) (2 () ()) ) ()) (8 (13 () ()) (4 () (1 () ()) ) ) )
Note that with this formulation all leaves of a tree are of the form (integer () () )
Since an empty tree has no root-to-leaf paths, any query as to whether a path exists whose sum is a specified integer in an empty tree must be answered negatively.
The Input
The input consists of a sequence of test cases in the form of integer/tree pairs. Each test case consists of an integer followed by one or more spaces followed by a binary tree formatted as an S-expression as describedabove. All binary tree S-expressions will be valid, but expressions may be spread over several lines and may contain spaces. There will be one or more test cases in an input file, and input is terminated by end-of-file.
The Output
There should be one line of output for each test case (integer/tree pair) in the input file. For each pairI,T (I represents the integer, T represents the tree) the output is the string yes ifthere is a root-to-leaf path in T whose sum is I and no if there is no path in T whose sum is I.
Sample Input
22 (5(4(11(7()())(2()()))()) (8(13()())(4()(1()())))) 20 (5(4(11(7()())(2()()))()) (8(13()())(4()(1()())))) 10 (3 (2 (4 () () ) (8 () () ) ) (1 (6 () () ) (4 () () ) ) ) 5 ()
Sample Output
yes no yes no
看代码你就懂了。
完整代码:
/*0.082s*/ #include<cstdio> int f(int n) { int ans, m; if (scanf(" (%d", &m)) { ans = f(n - m) + f(n - m); if (ans < 2) ans = 0; } else ans = !n; scanf(" )"); return ans; } int main() { int n; while (~scanf("%d", &n)) ///空树会返回1 puts(f(n) > 1 ? "yes" : "no"); return 0; }
相关文章推荐
- UVA 112 & POJ 1145 Tree Summing(数据结构,二叉树,栈)
- POJ 题目1145/UVA题目112 Tree Summing(二叉树遍历)
- POJ 题目1145/UVA题目112 Tree Summing(二叉树遍历)
- uva 112 - 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 oj 112 - Tree Summing
- UVa 112 Tree Summing
- scanf(" %c");(注意前面空格)
- UVA - 112 Tree Summing
- uva-112 Tree Summing
- UVA - 112 Tree Summing
- uva112-Tree Summing[动态建树]
- Uva 112 - Tree Summing
- Tree Summing - UVa 112 简单二叉树(丧病的数据输入)
- 数据结构 uva 112-Tree Summing