UVA Tree Summing

题目如下:

 Tree Summing 

Background

LISP was one of the earliest high-level programming languages and, withFORTRAN, is one of the oldest languages currently being used. Lists,which are the fundamental data structures in LISP, can easily be adaptedto represent other important data structures
such as trees.

This problem deals with determining whether binary trees represented asLISP S-expressions possess a certain property.

The Problem

Given a binary tree of integers, you are to write a program thatdetermines whether there exists a root-to-leaf path whosenodes sum to a specified integer. For example, in the tree shown belowthere 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-expressionshaving 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 apath exists whose sum is a specified integer in an empty treemust be answered negatively.

The Input

The input consists of a sequence of test cases in theform of integer/tree pairs. Each test caseconsists of an integer followed by one or more spaces followed by abinary tree formatted as an S-expression as described above. Allbinary tree S-expressions will
be valid, but expressions may bespread over several lines and may contain spaces.There will be one or more test cases in an input file, and input isterminated by end-of-file.

The Output

There should be one line of output for each test case (integer/treepair) in the input file. For each pair
I,T (I represents theinteger, T represents the tree) the output is the string
yes ifthere is a root-to-leaf path in T whose sum is I and
no ifthere 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

这道题我是先建树,再遍历。难点在于建树,因为输入比较麻烦,可以分几行输入,所以对空格要忽略,只有对空树的时候才忽略前导‘(’,若非空树,每遇到一个‘(’代表一个节点成功输入。

AC的代码如下;

UVA Tree Summing,布布扣,bubuko.com

时间: 2024-10-12 19:18:12

UVA Tree Summing的相关文章

UVa 112 - Tree Summing(树的各路径求和,递归)

题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=48 Tree Summing  Background LISP was one of the earliest high-level programming languages and, with FORTRAN, is one of the olde

UVA - 112 - Tree Summing (数的求和!栈的应用!)

UVA - 112 Tree Summing Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Tree Summing  Background LISP was one of the earliest high-level programming languages and, with FORTRAN, is one of the oldest

POJ 题目1145/UVA题目112 Tree Summing(二叉树遍历)

Tree Summing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8132   Accepted: 1949 Description LISP was one of the earliest high-level programming languages and, with FORTRAN, is one of the oldest languages currently being used. Lists, w

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 represe

Tree Summing[UVA-112]

Tree Summing UVA-112 Time Limit:3000ms 这个题主要还是考察对二叉树的理解,刚看到感觉挺简单的,但是做起来却是一个接着一个坑. 首先说一下思路吧.最先想到的是重建整个二叉树,然后对整个树遍历,求出所有从树根到叶子的和,再与题目要求的数进行比较.后来一想其实没必要重建整个树,因为在这个题目的输入下,重建整个树的过程就相当于一次DFS,DFS到底实际上就是达到叶子节点了.因此,一边处理输入一边直接计算求和就行了,满足公式:$s_c=s_p+v_c$,其中$s_c$

UVA Tree

题目如下: Tree  You are to determine the value of the leaf node in a given binary treethat is the terminal node of a path of least value from the root of thebinary tree to any leaf. The value of a path is the sum of values of nodesalong that path. Input

Groupon面经:Find paths in a binary tree summing to a target value

You are given a binary tree (not necessarily BST) in which each node contains a value. Design an algorithm to print all paths which sum up to that value. Note that it can be any path in the tree - it does not have to start at the root. http://stackov

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

二叉树基础练习

前序遍历(先根遍历):根,左子树,右子树 中序遍历:左子树,根,右子树后序遍历:左子树,右子树,根 先序遍历:ABDECF 中序遍历:DBEAFC 后序遍历:DEBFCA 层次遍历:ABCDEF UVA 112  Tree Summing 题目:给你一个数和一棵树,问是否存在根到叶子的路径使得路径上的数字和与已知数相等. 注意数据中可能有负数 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #i