首页 > 其他 > 详细

POJ1145 UVA112 Tree Summing【表达式二叉树】

时间:2019-02-24 12:08:11      阅读:163      评论:0      收藏:0      [点我收藏+]

Tree Summing
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 8560 Accepted: 2118

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.

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

Source

Duke Internet Programming Contest 1992,UVA 112

问题链接POJ1145 UVA112 Tree Summing
问题简述:(略)
问题分析
????LISP语言(函数式语言)的表达式二叉树问题。给定一个数,计算表达式二叉树从树根到叶子的和(叶子结点和),如果和与给定数相等则输出"yes",否则输出"no"。
????该问题虽然是一个二叉树的问题,但是关键在于表达式如何进行语法识别,递归子程序是识别表达式的一种有效办法。本题的表达式语法可以归结如下:
exp ::= ()
exp ::= (exp exp) | (integer exp exp)
????感觉题面似乎没有写清楚。按照这个语法, 进行识别即可。给定的树(字符串表示)是合法的,按照语法进行分析即可。
????到目前为止,网上的题解基本上都是歪招,没有按照原理来,不够严密。虽然采用建树来处理也可以,但是递归程序本身就可以实现树的遍历,而且可以在字符的基础上进行。应该说,本题解才是正解。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* POJ1145 UVA112 Tree Summing */

#include <iostream>
#include <cstdio>

using namespace std;

#include <stdio.h>
int tot;
bool flag;

char mygetchar()
{
    char c;
    while((c=getchar()) && (c==' ' || c=='\t' || c=='\n' || c==EOF));
    return c;
}

int tree(int sum, int lvl)
{
   char c;
   c = mygetchar();
   if(c == '(') {
       c = mygetchar();
       if(c == ')')     // empty tree :: ()
           return lvl;
       else {
           int a = 0;

           ungetc(c, stdin);    // tree :: (tree tree)
           if(c != '(')
               scanf("%d", &a);     // tree :: (integer tree tree)
           int k1 = tree(sum + a, lvl + 1);
           int k2 = tree(sum + a, lvl + 1);

           if(k1 == lvl + 1 && k2 == lvl + 1)       // 2个叶子结点
               if(sum + a == tot)
                   flag = true;

           while((c = mygetchar()) && c != ')');
           return 0;
       }
   }

   return 0;
}

int main()
{
    while(scanf("%d", &tot) != EOF) {
        flag = false;
        tree(0, 0);
        printf("%s\n", flag ? "yes" : "no");
    }
    return 0;
}

POJ1145 UVA112 Tree Summing【表达式二叉树】

原文:https://www.cnblogs.com/tigerisland45/p/10425645.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!