## 112 – Tree SummingTime limit: 3.000 seconds |

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.

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

1 2 3 4 5 6 7 8 |
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

1 2 3 4 |
yes no yes no |

输入一个值, 然后给一颗树, 最后判断树里面存不存在与给的值相等的路径长.

来一个小小的DFS.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
/******************************************************** * File Name: uvaoj112.cpp * Author: razrLeLe * Mail: razrlele@outlook.com * Homepage: https://yueyu.io * Created Time: Mon 29 Dec 2014 08:44:48 AM CST ******************************************************/ #include "vector" #include "set" #include "deque" #include "queue" #include "algorithm" #include "functional" #include "iostream" #include "cstdio" #include "cmath" #include "cstdlib" #include "string" #include "cstring" #include "string.h" #include "map" #include "cctype" #include "list" #include "stack" using namespace std; #define INF 0x3f3f3f3f #define LOCAL bool Yes; bool ScanTree(int sum, int aim) { static int num; char parentheses; cin >> parentheses;//输入右括号 bool judge = (cin>>num);//这个地方如果输入一个左括号的话就说明是叶子, 左括号会存在缓存区, cin返回一个false if(judge) { sum+=num; bool leaf1 = ScanTree(sum, aim); bool leaf2 = ScanTree(sum, aim); if(!leaf1 && !leaf2 && sum == aim) { Yes = true; } } cin.clear();//clear过后就可以继续利用cin缓存区的值了 cin >> parentheses; return judge; } int main() { #ifdef LOCAL freopen("/home/razrlele/build/data.txt", "r", stdin); freopen("/home/razrlele/build/out.txt", "w", stdout); #endif int T; while(~scanf("%d", &T)) { Yes = false; ScanTree(0, T); cout << (Yes?"yes":"no") << endl; } return 0; } |