Tree Summing

Uva 112 – Tree Summing

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(Irepresents the integer,Trepresents the tree) the output is the stringyesif there is a root-to-leaf path inTwhose sum isIandnoif there is no path inTwhose sum isI.

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

yesnoyesno

#include<stdio.h>#include<string.h>#include<ctype.h>#define MAXN 1000int main(){char ch, ch1, ch2, ch3, ch4;int digit[MAXN];int left, right, num, sum, flag, cnt, start, rear, success, fuhao;, &num) != EOF){memset(digit, 0, sizeof(digit));rear = left = right = sum = flag = cnt = 0;start = 1, success = 0, fuhao = 1;ch1 = ch2 = ch3 = ch4 = ;while(left != right || start){scanf(, &ch);|| ch == || ch == ){){if(isdigit(ch))digit[rear] = digit[rear]*);else fuhao = -1;flag = 1;}else if(flag){flag = 0;digit[rear] = fuhao*digit[rear];sum += digit[rear];fuhao = 1;rear++;}ch4 = ch3, ch3 = ch2, ch2 = ch1, ch1 = ch;&& ch3 == && ch2 == && ch1 == ){if(sum == num){success = 1;}ch4 = ch3 = ch2 = ch1 = ;}&& ch1 == ){sum -= digit[rear-1];digit[rear-1] = 0;rear–;}) left++;) right++;if(left != right) start = 0;}}););}return 0;}

解题思路:

为了增加难度,美国空间,题目可是在输入格式上花了点心思,不仅要在数字和括弧之间留下空格和换行游荡,还要将负数的符号和数字分开(我能告诉你我就是坑在这里吗)

首先要处理格式上的问题,这就要知道什么时候算是一个test case结束了,结果是:如果左括号 == 右括号 则表示一个case 结束;

接着存储数字而且同时求和,存储数字的原因是回到父子节点再遍历另一条子树时需要减去相应的节点值(如果到达节点时得到的和刚好是题目要求的,这时千万不要break输出)

如何判断到达叶子节点,最好是用四个char字符存储最近输入的四个字符(在过滤掉换行符和空格后)

人生就像是一场旅行,遇到的既有感人的,

Tree Summing

相关文章:

你感兴趣的文章:

标签云: