uva112 - Tree Summing
来源:互联网 发布:c语言大全 豆瓣 编辑:程序博客网 时间:2024/06/03 00:53
Tree Summing
Tree Summing
Tree Summing
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 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
原来不用建树也可以解决此问题,参考了https://github.com/igorbonadio/uva/blob/master/112/112.cpp,程序中Node函数的返回值:对于()而言,父节点到根节点的权与给出的值相等则返回true,否则返回false,对于非()一律返回false;而output的值表示当前节点为叶节点且满足相等的题目要求。
#include<iostream>#include<string>using namespace std;void Spaces(void){while(cin.peek() == ' ' || cin.peek() == '\n'){cin.ignore(1);}}bool Node(int sum, int s, bool &output){int val;int sign = 1;Spaces();cin.ignore(1);//(Spaces();if(cin.peek() == ')'){cin.ignore(1);//)return ((sum == s)?true:false);}Spaces();if(cin.peek() == '-'){cin.ignore(1);sign = -1;}cin >> val;val = val*sign;sum += val;int l = Node(sum, s, output);int r = Node(sum, s, output);if(l && r)output = true;Spaces();cin.ignore(1);//)return false;}int main(void){int s;bool outp;#ifndef ONLINE_JUDGEfreopen("f:\\infile.txt", "r", stdin);#endifwhile(cin >> s){outp = false;Node(0, s, outp);cout << ((outp == true) ? "yes" : "no") << endl;}return 0;}
- UVA112-Tree Summing
- uva112 - Tree Summing
- UVA112 Tree Summing
- uva112 Tree Summing
- uva112-Tree Summing[动态建树]
- uva112 Tree Summing(二叉树 + 栈)
- Tree Summing
- UVA112
- uva112
- UVA112
- uva112
- 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
- 第六周作业2
- 《Thinking in Algorithm》12.详解十一种排序算法
- 微信公众平台 微信公众平台互刷 加我增人气
- ARM平台的地址对齐问题
- 操作系统常见面试题
- uva112 - Tree Summing
- HashMap的底层实现
- 为什么要用散列表(哈希表,hashtable)
- 认证&密码文件
- java application突然中止
- 百度的奇迹---双搜索栏
- Linux之旅----系统篇
- SQLServer数据库学习总结
- 使用qq进行菜单截图