UVa 112 - Tree Summing
来源:互联网 发布:win10修复ubuntu引导 编辑:程序博客网 时间:2024/06/06 01:45
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 pair I,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 <cstdio>using namespace std;bool flag=false;void space(){while(cin.peek()==' '||cin.peek()=='\n')cin.ignore();}bool solve(int sum, int var){space();cin.ignore(); // (space();if(cin.peek()==')'){cin.ignore();// )return var==sum?true:false;}int sign=1;if(cin.peek()=='-'){sign=-1;cin.ignore();}space();int n;cin>>n;bool left = solve(sum, var+n*sign);bool right= solve(sum, var+n*sign);if(left&&right)flag = true;space();// ) 回溯忽略‘)’cin.ignore();return false;// 根结点无需判断}int main(){//freopen("in.txt","r",stdin);int sum;while(cin>>sum){flag = false;solve(sum, 0);cout << (flag?"yes":"no") << endl;}return 0;}
- uva 112 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
- UVa 112 - 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
- UVa 112 Tree Summing
- UVa 112 Tree Summing
- UVA 112 Tree Summing
- uva 112 Tree Summing
- (递归)某人写了n封信和n个信封,如果所有的信都装错了信封。求所有的信都装错信封共有多少种不同情况。
- 什么是排名软件?
- 按概率返回0-N之间的数字的解法思想
- 网络处理中TLV形式的不固定格式匹配
- C++第2周(春)项目2 有序的结构体数组
- UVa 112 - Tree Summing
- Valid Palindrome
- 二叉树中序遍历的非唯一性
- c# 自定义事件和委托
- 大二层网络还是重建会话层
- Overloading,overriding,and extending functions and events 重载,重写和扩展函数和事件
- 最短路自学总结
- C++第2周(春)项目3 文件+结构体实现实用系统
- 第21条:用函数对象表示策略