百练/ 北京大学2016研究生推免上机考试(校外)H: Tree Summing(栈)

该博客介绍了如何处理一个LISP表达式树,以确定是否存在一条从根到叶节点的路径,其节点之和等于给定的整数。通过解析LISP S-expressions来表示的二叉树,并利用栈进行括号匹配和路径求和,判断是否存在满足条件的路径。示例和解题思路均被详细说明。
摘要由CSDN通过智能技术生成

题目来源: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. 

输入

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值