您的位置:首页 > 其它

uva112 - Tree Summing

2014-04-14 23:26 344 查看





 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

yes
no
yes
no


原来不用建树也可以解决此问题,参考了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_JUDGE
freopen("f:\\infile.txt", "r", stdin);
#endif

while(cin >> s){
outp = false;
Node(0, s, outp);
cout << ((outp == true) ? "yes" : "no") << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: