<p class="pst">Description</p><span style="float:right"><a target=_blank target="_blank" href="http://7xjob4.com1.z0.glb.clouddn.com/67c692c07aa281611caec166fd4996f7"><img title="Download as PDF" alt="Download as PDF" src="http://uva.onlinejudge.org/components/com_onlinejudge/images/button_pdf.png" border="0" height="26" width="100" /></a></span><h2><span style="color:#0070E8;"><a target=_blank name="SECTION0001001000000000000000" href="http://acm.hust.edu.cn/vjudge/contest/view.action?cid=95584#problem/A">Background</a></span></h2><p> 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. </p><p> This problem deals with determining whether binary trees represented as LISP S-expressions possess a certain property. </p><h2><span style="color:#0070E8;"><a target=_blank name="SECTION0001002000000000000000" href="http://acm.hust.edu.cn/vjudge/contest/view.action?cid=95584#problem/A">The Problem</a></span></h2><p> 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. </p><p><img alt="picture25" src="http://7xjob4.com1.z0.glb.clouddn.com/fa599cee55ce652e1f84d9263009b1f0" align="BOTTOM" height="226" width="290" /></p><p> Binary trees are represented in the input file as LISP S-expressions having the following form. </p><pre><tt><em>empty tree</em> ::= ()
</tt><p><em>tree</em> ::= <em>empty tree</em><img alt="tex2html_wrap_inline118" src="http://7xjob4.com1.z0.glb.clouddn.com/6acd36e4f8ba6a36c5cf3d2bf6df81e1" align="MIDDLE" height="27" width="2" /> (integer <em>treetree</em>) </p>
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
yes no yes no
给你一棵树还有一个数
判断是否存在一条路径上的值相加的=这个数
第一次做树,还不是很会做,对递归也不熟悉看了很多网上的代码
下面这个是最简单的
思路也很清晰
把每条路径上的值分开加起来
逐一判断
这道题的有个坑点就是输入的时候有空格有换行
#include <iostream> #include <cstdlib> #include <cstdio> using namespace std; char input( void ) { char temp; scanf("%c",&temp); while ( temp == ' ' || temp == '\n' ) scanf("%c",&temp); return temp; } int deal( int v, int *leaf ) { int temp,value; scanf("%d",&value); temp = input(); int max = 0,l = 0,r = 0; if ( temp == '(' ) { if ( deal( v-value, &l ) ) max = 1; temp = input(); if ( deal( v-value, &r ) ) max = 1; temp = input(); if ( l && r ) max = (v==value); }else *leaf = 1; return max; } int main() { int n,temp; while ( ~scanf("%d",&n) ) { input(); if ( deal( n, &temp ) ) printf("yes\n"); else printf("no\n"); } return 0; }