UVA 112 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 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


题意:先输入一个n。根据输入。,。建一颗二叉树。。如果二叉树有一条路径上的权值和等于n 输出yes 没有就输出 no

这题思路很明确。建树,遍历。。 不过建树的时候比较麻烦,,要进行字符串的处理。。 由于可能有回车。所以不能用字符串读入了。。只能一个个字符读入。。

遇到左括号。就把左括号后面的数字转换成int型整数。直到遇到一个右括号或者左括号为止。然后把这个数字存入根节点,接着构造左右子树。。 如果遇到空格或者回车,忽略跳过。 如果一个左括号后面跟着一个右括号,则该节点为NULL。。

建树比较的麻烦。。不过树建好后就是简单的遍历如果有满足条件直接返回输出yes。。

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

int n;
int judge;

typedef struct Tree
{
    int node;
    int sum;
    struct Tree *l;
    struct Tree *r;
    Tree()
    {
	node = 0;
	sum = 0;
	l = NULL;
	r = NULL;
    }
} bitree;

int ju = 0;

void build (bitree *&T)
{
    int num = 0;
    char c;
    int f = 0;
    if (ju == 0)
	while (getchar() != '(');
    while (c = getchar())
    {
	if (isspace(c))
	    continue;
	if (c == '(' || c == ')')
	    break;
	if (c == '-')
	{
	    f = 1;
	    continue;
	}
	num *= 10;
	num += c - '0';
    }
    if (c == ')')
    {
	T = NULL;
	ju = 0;
    }
    else
    {
	ju = 1;
	if (f)
	    num = - num;
	T = new(bitree);
	T -> node = num;
	build(T -> l);
	build(T -> r);
    }

}
void dfs(bitree *T, int sum)
{
    if (judge == 1)
	return;
    if (T == NULL)
	return;
    sum += T -> node;
    if (T -> l)
	dfs(T -> l, sum);
    if (T -> r)
	dfs(T -> r, sum);
    if (sum == n && T -> l == NULL && T -> r == NULL)
	judge = 1;
}

void pre(bitree *T)
{
    if(T != NULL)
    {
	printf("%d", T -> node);
	pre(T -> l);
	pre(T -> r);
    }
}
int main()
{
   
 while (scanf("%d", &n) != EOF)
    {
	char c;
	judge = 0;
	bitree *T;
	build(T);
	while (c = getchar())
	{ 
	    if (c == '\n') 
		break; 
	    if (c == EOF) 
		break; 
	}
	//pre(T);
	dfs(T, 0);
	if (judge)
	    printf("yes\n");
	else
	    printf("no\n");

    }
    return 0;
}




你可能感兴趣的:(UVA 112 Tree Summing)