POJ 题目1145/UVA题目112 Tree Summing(二叉树遍历)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 8132 | Accepted: 1949 |
Description
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
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
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(二叉树遍历)的更多相关文章
- UVa 112 - Tree Summing(树的各路径求和,递归)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- UVa 112 Tree Summing
题意: 计算从根到叶节点的累加值,看看是否等于指定值.是输出yes,否则no.注意叶节点判断条件是没有左右子节点. 思路: 建树过程中计算根到叶节点的sum. 注意: cin读取失败后要调用clear ...
- PAT 1020 Tree Traversals[二叉树遍历]
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT甲题题解1099. Build A Binary Search Tree (30)-二叉树遍历
题目就是给出一棵二叉搜索树,已知根节点为0,并且给出一个序列要插入到这课二叉树中,求这棵二叉树层次遍历后的序列. 用结构体建立节点,val表示该节点存储的值,left指向左孩子,right指向右孩子. ...
- hdu1710(Binary Tree Traversals)(二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- PAT 甲级 1020 Tree Traversals (二叉树遍历)
1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- POJ 1145 Tree Summing
Tree Summing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7698 Accepted: 1737 Desc ...
- uva 1556 - Disk Tree(特里)
题目连接:uva 1556 - Disk Tree 题目大意:给出N个文件夹关系,然后依照字典序输出整个文件文件夹. 解题思路:以每一个文件夹名作为字符建立一个字典树就可以,每一个节点的关系能够用ma ...
随机推荐
- [HNOI2018]爆零记
Day 0 完全不知道做什么. 打了一个splay板子,还没调出来emmmmm 不想做题目,最后做的一题是[HNOI2016]的超(sha)难(bi)题网络. 当我希望省选能出一下树剖时,旁边的大佬跟 ...
- HDU 4398 Template Library Management (最优页面调度算法)
中等偏易题.操作系统理论中的最优页面调度算法,贪心.当需要淘汰某个模版时,淘汰掉当前手中在最远的将来才会被用到(或者以后永远不再用到)的那个. 代码: #include <iostream> ...
- 百度地图API的使用示例
刚刚工作的时候写过百度地图API文档,那时候没有记录到技术博客里面,今天在群里看见有个姑娘在问这个问题,重温了一遍,这个API还算好用.百度地图API,集成简单好用,全面,兼容问题,文档全面: 官方文 ...
- ArcGIS api for javascript——设置自定义范围和空间参考
描述 这个示例展示了在创建地图时如果定义一个自定义的范围和空间参考. 在 ArcGIS JavaScript API的1.0和1.1版本,任何要使用的地图服务图层都需要和地图的空间参考一致.1.2版本 ...
- maven中使用mybatis
1.Mybatis优缺点 优点: Mybatis实现了对Dao层的封装,隔离了SQL语句,便于管理,避免了像JDBC那样操作数据集,便于扩展等等. 缺点: Mybatis属于?半自动“ORM”,比Hi ...
- X的追求道路
X的追求道路 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描写叙述 X在大家的帮助下最终找到了一个妹纸,于是開始了漫漫的追求之路,那 ...
- 怎样使Dialog像Activity一样随心所欲的使用?
怎样使Dialog像Activity一样随心所欲的使用? android中的Dialog像是寄生在Activity中.在弹出Dialog时.因受到系统风格定义,导致Dialog怎么也不能如意,那么今天 ...
- 26. Intellij IDEA 启动项目ClassNotFoundException
转自:https://blog.csdn.net/zhw0596/article/details/81388147 使用Intellij IDEA 的过程中,新创建的项目启动时报 严重: Error ...
- PL/SQL Developer怎么连接远程数据库
首先打开电脑,到PL/SQL安装的指定目录 [D:\app\DZL\product\10.2.0\dbhome_1\NETWORK\ADMIN]或者[D:\oracle\product\10.2.0\ ...
- jQuery - 设置内容和属性 设置内容 - text()、html() 以及 val() , 设置属性 - attr()
jQuery - 设置内容和属性 设置内容 - text().html() 以及 val() text() - 设置或返回所选元素的文本内容 html() - 设置或返回所选元素的内容(包括 HTM ...