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 (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<stdio.h> #include<string.h> #include<ctype.h> #define MAXN 1000 int main() { char ch, ch1, ch2, ch3, ch4; int digit[MAXN]; int left, right, num, sum, flag, cnt, start, rear, success, fuhao; while(scanf("%d", &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("%c", &ch); if(isdigit(ch) || ch == '(' || ch == ')' || ch == '-') { if(isdigit(ch) || ch == '-') { if(isdigit(ch)) digit[rear] = digit[rear]*10 + (ch - '0'); 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; if(ch4 == '(' && ch3 == ')' && ch2 == '(' && ch1 == ')') { if(sum == num) { success = 1; } ch4 = ch3 = ch2 = ch1 = '?'; } else if(ch2 != '(' && ch1 == ')') { sum -= digit[rear-1]; digit[rear-1] = 0; rear--; } if(ch == '(') left++; else if(ch == ')') right++; if(left != right) start = 0; } } if(success) printf("yes\n"); else printf("no\n"); } return 0; }
解题思路:
为了增加难度,题目可是在输入格式上花了点心思,不仅要在数字和括弧之间留下空格和换行游荡,还要将负数的符号和数字分开(我能告诉你我就是坑在这里吗)
首先要处理格式上的问题,这就要知道什么时候算是一个test case结束了,结果是:如果左括号 == 右括号 则表示一个case 结束;
接着存储数字而且同时求和,存储数字的原因是回到父子节点再遍历另一条子树时需要减去相应的节点值(如果到达节点时得到的和刚好是题目要求的,这时千万不要break输出)
如何判断到达叶子节点,最好是用四个char字符存储最近输入的四个字符(在过滤掉换行符和空格后)
如果刚好是“(” “)” “(” “)”那么就可以判断这时到达了叶子节点(这时要注意:判断后将这四个字符初始化,因为涉及到后面可以会出现下两个字符是“(” “)” <隔壁一棵 子树节点不存在时>)
除去上面给这种情况,还要判断最近两个字符,其中当前的为“)” 而另一字符不是“(”的情况,这时表示要开始往回走,这时要做相应的减
另外的情况就不用再处理了,只是为了记录“(” “)” 的数量而判断case 的结束
个人情况:
一开始想用比较复杂的形式表达节点,将孩子节点,括号出现的情况都考虑到了但后面越觉得没有必要,考虑了很久(究竟有多久我也不知道了。。)越觉得很多东西可以不要的,删删减减最后才知道代码其实还是很简单的
涨经验了
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>