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 (I represents the integer, T represents the tree) the output is the stringyes if there is a root-to-leaf path in T whose sum is I andno 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
看到输入的形式,又是包含空节点的先序遍历,果断用递归,因为偷懒了所以无法直接知道某个节点是否为叶子节点,所以做的时候开个辅助的数组记录每个节点它下面的空节点数,有2个的为叶子节点;
#include<stdio.h> #define max 20000 int ans,postion,flag,a[max],point[max],leaf[max]; //position 表示当前这个节点在a数组(刚才按题目输入顺序)中的位置,point空节点值为1,否则为0 void sort(int answer,int father) //father 当前节点的父亲节点在a数组中的位置 {int k1,k2,Father=postion; if (point[postion]==1) {++postion; k1=point[postion]; sort(answer+a[postion],Father); //递归处理a数组中的数据,偷懒不建树。 ++postion; k2=point[postion]; sort(answer+a[postion],Father); } else {++leaf[father]; if ((leaf[father]==2)&&(ans==answer)) {flag=1;} //只有叶子节点,算出来的值才是某条路径的值,叶子节点就是,它作为父亲节点时,包含2个空节点 } } int main() {int l,f,pos,num,s1,s2,i,j,sum; char ch,s[max]; while (scanf("%d",&ans)!=EOF) { s1=0; s2=0; l=0; while ((s1+s2==0)||(s1!=s2)) //第一次输入预处理,只取出左右括号数字和负号,以左右括号相等为结束条件 {scanf("%c",&ch); if (ch=='-') {s[l]=ch;++l;} if (ch=='(') {s[l]=ch;++l;++s1;} if (ch==')') {s[l]=ch;++l;++s2;} if ((ch>='0')&&(ch<='9')) {s[l]=ch;++l;} } num=0; pos=1; sum=0; f=0; flag=0; for (i=0;i<l;i++) //第二次数据处理,取出所有数字 {if (s[i]=='-') pos=-1; if ((s[i]>='0')&&(s[i]<='9')) {f=1;num=num*10+s[i]-'0';} if ((s[i]==')'||s[i]=='(')&&(f==1)) {f=0;++sum;a[sum]=num*pos; point[sum]=1; num=0;pos=1;} if ((s[i-1]=='(')&&(s[i]==')')) { ++sum;a[sum]=0; point[sum]=0; pos=1;} } for (i=1;i<=sum;i++) leaf[i]=0; postion=1; if (point[1]==1) sort(a[1],1); if (flag) printf("yes\n"); else printf("no\n"); } return 0; }