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
这一题题意和思路都不是很难,不用建立一棵二叉树,直接进行遍历并记录节点之前包括该节点的元素之和。不过因为空格的原因,输入处理很麻烦,以前竟不知道cin.peek()和cin.ignore(),还有cin.putback()函数。看了https://github.com/igorbonadio/uva/tree/master/112上的代码才知道这回事,比直接取字符判断方便多了。这一题关键要注意空格,负号的处理,还有0()的情况。
以下是代码:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int var;
bool ans;
void rmspace()
{
while(cin.peek()==' '||cin.peek()=='\n')
cin.ignore(1);
}
bool node(int sum)
{
int v,sgn =1;
rmspace();
cin.ignore(1);// '('
rmspace();
if(cin.peek()==')')
{
cin.ignore(1); //")"
return sum==var;
}
if(cin.peek()=='-')
{
cin.ignore(1);
sgn = -1;
}
rmspace();
cin>>v;
v *= sgn;
sum+=v;
bool l = node(sum);
bool r = node(sum);
if(l&&r)
ans = true;
rmspace();
cin.ignore(1);// ')'
return false;//仅上对叶子进行判断是否等于var,非叶子用false直接跳过
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(cin>>var)
{
ans = 0;
node(0);
cout<<(ans?"yes":"no")<<endl;
}
}
还有测试数据:
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 ()
0 ()
5 (5 () ())
5 ( 5 () () )
5 (1 (3 () ()) (4 () ()))
5 (18 ( - 13 ( ) ( ))())
0 (1 ()(-2 () (1()()) ) )
2 (1 () (1 () (1 () () ) ) )
10 (5 () (5 () (5 () (5 () (4 () () ) ) ) ) )
10 (5 () (5 () (5 () (5 ( 3 () () ) (4 () () ) ) ) ) )
20 (5 () (5 () (5 () (5 () (4 () () ) ) ) ) )
Sample Output
yes
no
yes
no
no
yes
yes
yes
yes
yes
no
no
no
no
注:测试数据来自crystal_yi