UVA 112 (13.08.19)
来源:互联网 发布:nginx 目录访问权限 编辑:程序博客网 时间:2024/06/05 22:58
Tree Summing
Background
LISP was one of the earliest high-level programming languages and, withFORTRAN, is one of the oldest languages currently being used. Lists,which are the fundamental data structures in LISP, can easily be adaptedto represent other important data structures such as trees.
This problem deals with determining whether binary trees represented asLISP S-expressions possess a certain property.
The Problem
Given a binary tree of integers, you are to write a program thatdetermines whether there exists a root-to-leaf path whosenodes sum to a specified integer. For example, in the tree shown belowthere are exactly four root-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-expressionshaving 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 apath exists whose sum is a specified integer in an empty treemust be answered negatively.
The Input
The input consists of a sequence of test cases in theform of integer/tree pairs. Each test caseconsists of an integer followed by one or more spaces followed by abinary tree formatted as an S-expression as described above. Allbinary tree S-expressions will be valid, but expressions may bespread over several lines and may contain spaces.There will be one or more test cases in an input file, and input isterminated by end-of-file.
The Output
There should be one line of output for each test case (integer/treepair) in the input file. For each pairI,T (I represents theinteger, T represents the tree) the output is the stringyes ifthere is a root-to-leaf path in T whose sum is I andno ifthere 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
yesnoyesno
题意:
规则是这样的: ( 根 ( 左树 ) ( 右树 ) )
内有嵌套~
思路: 利用递归括号是左右配对的
收获及要点: 认识cin.clear()的使用, 对递归的理解
AC代码:
#include<stdio.h>#include<iostream>using namespace std;int sum, flag;int DFS(int n) { int date; char ch; cin>>ch; cin>>date; if(cin != 0) { n = n + date; int left = DFS(n); int right = DFS(n); if(!left && !right && !flag) { if(n == sum) flag = 1; } cin>>ch; return 1; } else { cin.clear(); cin>>ch; return 0; }}int main() { while(cin >> sum) { flag = 0; DFS(0); if(flag) printf("yes\n"); else printf("no\n"); } return 0;}
- UVA 112 (13.08.19)
- uva 112
- Uva 112
- uva 112
- uva 112
- uva 112
- UVA 112
- UVA 784 (13.08.09)
- UVA 401 (13.08.01)
- UVA 10361 (13.08.01)
- UVA 537 (13.08.01)
- UVA 409 (13.08.01)
- UVA 465 (13.08.02)
- UVA 10494 (13.08.02)
- UVA 10474 (13.08.04)
- UVA 156 (13.08.04)
- UVA 400 (13.08.05)
- UVA 10194 (13.08.05)
- uva 10720
- assembly language programm--HelloWorld
- SQL 26字母进位SQL
- 第六章 磁盘管理
- MFC 多线程及线程同步
- UVA 112 (13.08.19)
- 负载均衡的几种实现技术
- 九种常用排序的性能分析总结
- (转)CString转int
- 在IntelliJ下如何做parameterize method的重构
- 第七章 进程管理
- hdu4607 Park Visit (树直径)
- 不停的点击指定位置
- sqlplus 输出结果排版