UVa 112 Tree Summing (scanf()去空格&递归&二叉树遍历)

112 - Tree Summing

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=48

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 pairI,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

看代码你就懂了。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索scanf
, input
, integer
, tree
, echarts tree
, binary
, erkle tree
, jqueryligerui tree
, expression tree
, of
The
uva 112、scanf 空格、scanf遇到空格、scanf输入字符串 空格、scanf输入空格,以便于您获取更多的相关知识。

时间: 2024-08-08 07:08:21

UVa 112 Tree Summing (scanf()去空格&递归&二叉树遍历)的相关文章

uva 112 Tree Summing

点击打开链接 题目意思:给定一个字符串,把字符串里面的数字建成一颗树,数字有可能是负号,所以这一题其实有很多可以学的,比如怎样输入会有多行,还有怎么建立一颗树,等等. 解题思路:我们用一个字符数组来存储读入的字符串,用一个栈来存储括号,判断如果栈为空并且读到换行那么就退出.我们可以先把根节点建好,建根节点调用creatnode()函数,注意数字可能是1234    -1234这些情况,所以判断.然后我们在从根节点后面的位置开始变量字符数组,如果是('(')则判断下一额是数字还是右括号,如果是数字

jQuery去空格

  <input id="txtUserName" name="txtUserName" value=""/>     给input输入的值去空格:$.trim($('#txtUserName').val())   注:只能去掉左右两边空格,中间的空格去不掉.     原帖地址:http://www.haogongju.net/art/1095296 

为什么没有去空格

问题描述 <!DOCTYPEHTML><html><head><title>NewDocument</title><metaname="Generator"content="EditPlus"><metaname="Author"content=""><metaname="Keywords"content="

获取服务器传来的数据 用JS去空格的正则表达式_javascript技巧

今天早上到现在,一直在搞一个很愚蠢的问题,竟然一直没发现 如果$str=""; $str = "$str-$sno"; 这样下来,$str前面会有个空格,js获取此值后,必须去掉空格 去空格函数rstr=rstr.replace(/(^\s*)|(\s*$)/g,""); 另: 去左空格replace(/(^\s*)/g, ""); 去右空格replace(/(\s*$)/g, ""); 另外,如果进行程序

【PHP】字符串去空格并将每个单词首字母转换成大写de多种解法

问题描述 编写camel_case方法,实现将字符串去掉空格,并将每个单词的第一个字母转换成大写. 例如: camel_case("hello case"); // => "HelloCase" camel_case("camel case word"); // => "CamelCaseWord" 分析 ucfirst 数组 解决方案 数组元素转换大写 function camel_case(string $s)

easyui textbox 去空格

问题描述 easyui textbox 去空格 有谁知道easyui的textbox 怎么在输入空格的时候去掉,谢谢 解决方案 easyui-textbox方法失效问题 解决方案二: 我觉得可以加一个事件,当输入完成的时候,replaceAll一下,将空格置换掉.

php去空格trim无效用str_replace实现

平时过虑空格,一般都是用trim,今天发现,中间的空格去不掉.倒,查了说明才知道,trim只能去两头的,比如$abc =  a b c; ,用trim只能把a前面c后面的空格去掉,但是b前后的空格怎么办呢. str_replace出场了,这样就行了  代码如下 复制代码 str_replace(' ','',$abc). 需要去空格的同学们小心了,php去空格,trim不行,str_replace行了,但是trim可以删除头空格了如下 例  代码如下 复制代码 <?php trim 去除一个字符

UVa 112:Tree Summing 二叉树构造, 二叉树路径和

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=48&mosmsg=Submission+received+with+ID+10280379 题目类型: 数据结构, 二叉树 加强版样例输入: 22 (5(4(11(7()())(2()()))()) (8(13()())(4()(1()())))

UVa 152 Tree&#039;s a Crowd (暴力)

152 - Tree's a Crowd Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=98&page=show_problem&problem=88 Dr William Larch, noted plant psychologist and inventor of the phrase ``Think like