题目来源:http://qwsfsx.openjudge.cn/level3/1145/
1145:TreeSumming
总时间限制: 1000ms 内存限制: 65536kB
描述
LISPwas one of the earliest high-level programming languages and, with FORTRAN, isone of the oldest languages currently being used. Lists, which are thefundamental data structures in LISP, can easily be adapted to represent otherimportant data structures such as trees.
This problem deals with determining whether binary trees represented as LISPS-expressions possess a certain property.
Given a binary tree of integers, you are to write a program that determineswhether there exists a root-to-leaf path whose nodes sum to a specifiedinteger. For example, in the tree shown below there are exactly fourroot-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 thefollowing 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 pathexists whose sum is a specified integer in an empty tree must be answerednegatively.
输入