uva-112 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 pair I,T (I represents the integer, Trepresents 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<deque> #include<algorithm> #include<cstdio> #include<stack> #include<string> #include<vector> #include<map> #include<sstream> #include<cctype> #include<queue> #include<cstdlib> #include<cstring> using namespace std; struct node { int data; node*left,*right; node(int a):data(a),left(0),right(0) {} }; void judge(node*root,int sum,vector<int>& v) { if(root) { if(root->left==root->right) { //cout<<root->data+sum<<endl; v.push_back(root->data+sum); } judge(root->left,root->data+sum,v); judge(root->right,root->data+sum,v); } } void see(node*root) { if(root) { cout<<root->data<<endl; see(root->left); see(root->right); } } int main() { int num; char c; while(cin>>num) { stack<int> st; stack<node*>s; while(1) { c=getchar(); if(c==' '||c=='\n')continue; if(c=='(') { st.push(c); continue; } if(c=='-') { while(c=getchar()) if(c!=' '&&c!='\n')break; st.push(0-(c-'0')); continue; } if(isdigit(c)) { if(st.top()!='('&&st.top()!=')') { int num=st.top(); int flag=num>0?1:-1; st.pop(); st.push((abs(num)*10+c-'0')*flag); } else st.push(c-'0'); continue; } if(c==')') { if(st.top()!='(') { node*root=new node(st.top()); st.pop(); root->right=s.top(); s.pop(); root->left=s.top(); s.pop(); s.push(root); } else s.push(0); st.pop(); if(st.empty())break; } } //cout<<"********"<<endl; //see(s.top()); vector<int>v; bool flag=0; judge(s.top(),0,v); for(int i=0;i<v.size();i++) if(num==v[i]){flag=1;break;} if(flag)cout<<"yes"<<endl; else cout<<"no"<<endl; } return 0; } //10 (1(2(3(4()())())())())