Tree Summing
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 7811 | Accepted: 1786 |
Description
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.
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.
This problem deals with determining whether binary trees represented as LISP S-expressions possess a certain property.
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.
![](https://i-blog.csdnimg.cn/blog_migrate/418ec1f1c0429badc6a8028aa0ce65ca.gif)
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.
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.
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
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
二叉树重建。。。
不过好多人都说不用建二叉树,我用的是二叉树重建,锻炼自己的数据结构编写能力
我对输入的处理就是把'('、')'、'-'、数字插入到一个string中,这样再冲string中取数
我的代码写的太挫了,在poj上过了,但是在UVa上就各种过不了,和袁朗学长一起写的,他的也是poj上能过,UVa上过不了
如果哪位朋友发现问题还请告知
代码如下:
#include <map>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define esp 1e-9
#define MAXN 100010
#define ll long long
#define INF 0x7FFFFFFF
#define BUG system("pause")
#define SW(a,b) a^=b;b^=a;a^=b;
using namespace std;
struct TNode{
int v;
int left;
int right;
int parent;
}Tree[MAXN];
int a[MAXN];
int array[MAXN];
string tree;
int leaf = 1;
int root = 0;
int flag = 0;
int newnode = 0;
int arrayindex = 0;
void buildTree(int index){
flag++;
if(flag > index) return;
int parent = root;
if(Tree[root].left == INF){
Tree[root].left = a[leaf];
// cout << "Tree[" << root << "].v = " << Tree[root].v;
// cout << "\tTree[" << root << "].left = " << Tree[root].left << endl;
if(a[leaf] != -INF){
newnode++;
root = newnode;
Tree[root].v = a[leaf];
Tree[root].parent = parent;
}
leaf++;
} else if(Tree[root].right == INF){
Tree[root].right = a[leaf];
// cout << "Tree[" << root << "].v = " << Tree[root].v;
// cout << "\tTree[" << root << "].right = " << Tree[root].right << endl;
if(a[leaf] != -INF){
newnode++;
root = newnode;
Tree[root].v = a[leaf];
Tree[root].parent = parent;
} else root = Tree[root].parent;
leaf++;
} else {
root = Tree[root].parent;
}
// cout << "newnode = " << newnode << endl;
buildTree(index);
}
void buildarray(TNode Tree[]){
for(int i=0; i<=newnode; ++i){
if(Tree[i].parent != -1)
Tree[i].v += Tree[Tree[i].parent].v;
if(Tree[i].left==-INF && Tree[i].right==-INF){
array[arrayindex++] = Tree[i].v;
// cout << "Tree[" << i << "].v = " << Tree[i].v << endl;
}
}
}
int main(void){
// freopen("out.txt", "w", stdout);
int n;
while(cin >> n){
tree.clear();
for(int i=0; i<MAXN; ++i){
Tree[i].v = INF;
Tree[i].left = INF;
Tree[i].right = INF;
Tree[i].parent = INF;
}
while(getchar() != '(');
tree.push_back('(');
leaf = 1;
root = 0;
flag = 0;
newnode = 0;
arrayindex = 0;
char ch;
int count = 1;
while(count){
ch = getchar();
if(ch == '('){
count++;
tree.push_back(ch);
}
else if(ch == ')'){
count--;
tree.push_back(ch);
} if(ch == '-')
tree.push_back(ch);
else if(isdigit(ch))
tree.push_back(ch);
}
// cout << "tree : " << tree << endl;
int num = 0;
int index = 0;
int minus = 0;//表示负号出现与否
for(int i=0; i<tree.size(); ++i){
if(tree[i]=='-' && isdigit(tree[i+1]))
minus = 1;
else if(tree[i] == ')'){
if(tree[i-1] == '('){
a[index++] = -INF;
} else if(isdigit(tree[i-1])){
a[index++] = num;
minus = 0;
num = 0;
}
} else if(isdigit(tree[i])){
if(minus == 1){
num = num*10-(tree[i]-'0');
} else num = num*10+(tree[i]-'0');
} else if(tree[i] == '('){
if(isdigit(tree[i-1])){
a[index++] = num;
minus = 0;
num = 0;
}
}
}
// for(int i=0; i<index; ++i){
// cout << "a[" << i << "] = " << a[i] << endl;
// }
if(index > 0){
Tree[0].v = a[0];
Tree[0].parent = -1;
buildTree(index);
buildarray(Tree);
}
// cout << "index = " << index << endl;
// cout << "\n\n\n\n" << endl;
int ok = 0;
for(int i=0; i<arrayindex; ++i){
if(array[i] == n){
ok = 1;
break;
}
// cout << array[i] << endl;
}
if(ok == 1){
cout << "yes" << endl;
} else cout << "no" << endl;
}
return 0;
}