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

题目大意:给出一个二叉树的表达式,问有没有一个路径和是n

ac代码

Problem: 1145		User: kxh1995
Memory: 164K Time: 0MS
Language: C++ Result: Accepted
#include<stdio.h>
int tree_sum(int n)
{
int ans=0,m;
if(scanf(" (%d",&m))
{
ans=tree_sum(n-m)+tree_sum(n-m);
if(ans<2)
ans=0;
}
else
ans=!n;
scanf(" )");
return ans;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(tree_sum(n))
printf("yes\n");
else
printf("no\n");
}
}

POJ 题目1145/UVA题目112 Tree Summing(二叉树遍历)的更多相关文章

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

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  2. UVa 112 Tree Summing

    题意: 计算从根到叶节点的累加值,看看是否等于指定值.是输出yes,否则no.注意叶节点判断条件是没有左右子节点. 思路: 建树过程中计算根到叶节点的sum. 注意: cin读取失败后要调用clear ...

  3. PAT 1020 Tree Traversals[二叉树遍历]

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  4. PAT甲题题解1099. Build A Binary Search Tree (30)-二叉树遍历

    题目就是给出一棵二叉搜索树,已知根节点为0,并且给出一个序列要插入到这课二叉树中,求这棵二叉树层次遍历后的序列. 用结构体建立节点,val表示该节点存储的值,left指向左孩子,right指向右孩子. ...

  5. hdu1710(Binary Tree Traversals)(二叉树遍历)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  6. PAT 甲级 1020 Tree Traversals (二叉树遍历)

    1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...

  7. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  8. POJ 1145 Tree Summing

    Tree Summing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7698   Accepted: 1737 Desc ...

  9. uva 1556 - Disk Tree(特里)

    题目连接:uva 1556 - Disk Tree 题目大意:给出N个文件夹关系,然后依照字典序输出整个文件文件夹. 解题思路:以每一个文件夹名作为字符建立一个字典树就可以,每一个节点的关系能够用ma ...

随机推荐

  1. WHU 1537 Stones I

    题目见: http://acm.whu.edu.cn/land/problem/detail?problem_id=1537 这个题相当无语,学长给的解法是:枚举取的个数k,然后对每个k贪心,取其中的 ...

  2. iOS 卖票中多线程分析;

    注意:(主要一个加锁机制)

  3. Dubbo源代码分析(三):Dubbo之服务端(Service)

    如上图所看到的的Dubbo的暴露服务的过程,不难看出它也和消费者端非常像,也须要一个像reference的对象来维护service关联的全部对象及其属性.这里的reference就是provider. ...

  4. html5的代码验证

    http://html5.validator.nu/ http://validator.w3.org/#validate_by_uri

  5. apicloud,aliyunlive,测试成功

    1.推流 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  6. git帮助命令

    git帮助命令 零.自己实例 cd D://software/code/PHP/phpStudy/PHPTutorial/WWW/github/m_Orchestrate git checkout - ...

  7. 方便查看线程状况的jsp页面

    此方法来自深入理解java虚拟机一书,用作管理员页面,可以随时用浏览器查看线程堆栈 <%@ page language="java" import="java.ut ...

  8. 威联通NAS 网站无法登录,可以ssh情况下重启设备方法

    步骤: 1.VPN登录NAS 2.PUTTY SSH登录设备 3.reboot设备 等待重启约5分钟.

  9. 洛谷 P1716 双调序列

    P1716 双调序列 题目描述 电脑组的童鞋们经常玩一些智力PK小游戏,某月某日,发源于小朋友又发明了一种新的序列:双调序列,所谓的双调呢主要是满足如下条件描述: 假定有n(n<=1000)个整 ...

  10. XCode6报数组越界错误的问题

    今天碰到一个非常奇葩的问题, 调试了半天: 错误:"index 0 beyond bounds for empty array",  意思就是说数据源数组为nil, 所以你调用直接 ...