UVaOJ112---Tree Summing
来源:互联网 发布:sleep和wait的区别JAVA 编辑:程序博客网 时间:2024/06/06 00:49
112 - Tree Summing
Time limit: 3.000 seconds
112 - Tree Summing
Time limit: 3.000 secondsBackground
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 to represent 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 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-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 described above. 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 if there 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
yesnoyesno
#include<iostream>//#include<stdio.h>using namespace std;typedef struct Tree{ int w; Tree *l, *r;}Tree, *pTree;int sum, flag, d;void make_tree(pTree &p);void preTraverse(pTree p);int main(){ while(cin >> d) { char c; pTree p = NULL; sum = 0; flag = 0; cin >> c; if(c == '(') { make_tree(p); } if(p == NULL) { cout << "no" << endl; continue; } preTraverse(p); if(flag == 1) { cout << "yes" << endl; } else { cout << "no" << endl; } } return 0;}void preTraverse(pTree p){ if(flag == 1) { return; } if(p == NULL) { return; } sum += p->w; if(p->l == NULL &&p->r == NULL) { if(sum == d) { flag = 1; return; } } else { preTraverse(p->l); preTraverse(p->r); }sum -= p->w;}void make_tree(pTree &p){ char c; int i; if(cin >> i) //输入成果说明是int { p = new Tree; p->w = i; p->l = NULL; p->r = NULL; } else //输入的是) { cin.clear(); cin >> c; return; } cin >> c; if(c == '(') { make_tree(p->l); } else { return; } cin >> c; if(c == '(') { make_tree(p->r); } else { return; } cin >> c;}
- UVaOJ112---Tree Summing
- Tree Summing
- POJ 1145 Tree Summing
- poj 1145 Tree Summing
- 112 - Tree Summing***
- UvaOJ 112 - Tree Summing
- uva 112 Tree Summing
- uva 112 - Tree Summing
- uva 112 - Tree Summing
- UVa 112 - Tree Summing
- 112 - Tree Summing
- uva 112 - Tree Summing
- UVA112-Tree Summing
- uva 112 Tree Summing
- uva_112-Tree Summing
- UVA 112 Tree Summing
- UVa 112 - Tree Summing
- UVa 112 - Tree Summing
- Flex 中正则表达式的使用
- vim使用
- jQuery各种动画效果
- centos 安装vsftp 绝对成功包括目录权限配置
- 2013年4月,5月要做的事情
- UVaOJ112---Tree Summing
- 《学习OpenCV》练习题第三章第二题
- 浅谈facebook服务器架构
- 考试评分系统 数据库表 备份一下
- UVaOJ548---Tree
- 函数指针
- 《学习OpenCV》练习题第三章第三题
- 苏一辰之8皇后问题
- remove the duplicate(重复) characters from a string