您的位置:首页 > 其它

uva 112 Tree Summing(建树+遍历)

2013-07-31 13:41 447 查看


 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

题目大意:给出一颗树,每个节点有它自己的value,从根节点到每的叶子节点的路径上所有value的和为该叶子的值,判断这棵树上是否有叶子的值等于所给的数值。
解题思路:建树(建议用指针的方法建),这也是这道题目的难点吧。建完树之后将树遍历,将所有叶子的值保存,最后匹配。

#include<stdio.h>
#include<string.h>

#define N 10005
struct rode{
int value;
rode *lift;
rode *right;
rode(){
value = 0;
lift = right = NULL;
}
};
int n, num
;

rode *build(){
char c;
int cnt = 0, bo = 0;
rode *now;
now = new rode;

while (scanf("%c", &c)){
if (c >= '0' && c <= '9'){
now -> value = now -> value * 10 + c - '0';
}
else if (c == '(' && cnt == 0){
now -> lift = build();
cnt++;
}
else if (c == '(' && cnt == 1){
now -> right = build();
cnt++;
}
else if(c == ')')
break;
else if (c == '-')
bo = 1;
}

if (bo)
now -> value = - now -> value;
if (now -> value == 0 && cnt == 0)
return NULL;
else
return now;
}

void BFS(rode * now, int sum){
if (now == NULL)
return;
sum += now -> value;

if (now -> lift == NULL && now -> right == NULL)
num[n++] = sum;
else{
if (now -> lift != NULL)
BFS(now -> lift, sum);
if (now -> right != NULL)
BFS(now -> right, sum);
}
}
void delete_tree(rode * now){
if (now == NULL)
return ;
if (now -> lift != NULL)
delete_tree(now -> lift);
if (now -> right != NULL)
delete_tree(now -> right);
delete now;
}
int main(){
int sum;
char c;
rode *head;

while (scanf("%d", &sum) != EOF){
// Init.
memset(num, 0, sizeof(num));
head = NULL;
n = 0;

// Build tree;
while (scanf("%c", &c) != EOF){
if (c == '('){
head = build();
break;
}
}

// BFS.
BFS(head, 0);

// Judge.
int i;
for (i = 0; i < n; i++)
if (num[i] == sum)
break;
if (i == n)
printf("no\n");
else
printf("yes\n");

// Delete.
delete_tree(head);
}
return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: