UVa 112 Tree Summing
题意:
计算从根到叶节点的累加值,看看是否等于指定值。是输出yes,否则no。注意叶节点判断条件是没有左右子节点。
思路:
建树过程中计算根到叶节点的sum。
注意:
cin读取失败后要调用clear恢复,否则后面无法正常读取。
注意空树都要输出no
最初代码如下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
#include<queue>
#include<assert.h>
using namespace std; int g_sum;
bool ok; //build tree & dfs
struct node
{
node(node* left=0, node* right=0, int value=0): l(left), r(right), v(value) {}
node* l;
node* r;
int v;
}*root; node* build_tree(int sum)
{
node* nd=0;
char c;
cin>>c;
assert(c=='('); int v;
if(cin>>v)
{
nd=new node;
nd->v=v;
//left
nd->l=build_tree(sum+v);
//right
nd->r=build_tree(sum+v);
if(!nd->l && !nd->r && (sum+v==g_sum))
{
ok=true;
}
}
else//空节点,没有int,从错误中恢复
{
//if(sum==g_sum)
// ok=true;
cin.clear();
}
cin>>c;
assert(c==')');
return nd;
} void bfs()
{
if(!root)
return;
queue<node*> q;
q.push(root);
while(!q.empty())
{
node* nd=q.front(); q.pop();
cout<<nd->v<<" ";
if(nd->l) q.push(nd->l);
if(nd->r) q.push(nd->r);
}
} void delete_tree(node *root)
{
if(root)
{
delete_tree(root->l);
delete_tree(root->r);
}
} int main()
{
while(cin>>g_sum)
{
ok=false;
root=build_tree(0);
//bfs();
delete_tree(root);
if(ok)
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
} return 0;
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
可把显式的建树过程改为隐式的,不用建立node
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
#include<queue>
#include<assert.h>
using namespace std; //注意空树都要输出no int g_sum;
bool ok; //空节点返回0,非空返回1
int build_tree(int sum)
{
int empty=true;
char c;
cin>>c;
assert(c=='('); int v;
if(cin>>v)
{
empty=false;
//left
int left=build_tree(sum+v);
//right
int right=build_tree(sum+v);
//叶节点:左右子节点都是空的
if(!left && !right)
{
// cout<<sum+v<<endl;
if(sum+v==g_sum)
ok=true;
} }
else//空节点,没有int,从错误中恢复
{
cin.clear();
}
cin>>c;
assert(c==')');
return !empty;
} int main()
{
while(cin>>g_sum)
{
ok=false;
build_tree(0);
if(ok)
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
} return 0;
}
UVa 112 Tree Summing的更多相关文章
- UVa 112 - Tree Summing(树的各路径求和,递归)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- POJ 题目1145/UVA题目112 Tree Summing(二叉树遍历)
Tree Summing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8132 Accepted: 1949 Desc ...
- UVA.548 Tree(二叉树 DFS)
UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...
- POJ 1145 Tree Summing
Tree Summing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7698 Accepted: 1737 Desc ...
- uva 558 tree(不忍吐槽的题目名)——yhx
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
- 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 alg ...
- UVa 536 Tree Recovery | GOJ 1077 Post-order (习题 6-3)
传送门1: https://uva.onlinejudge.org/external/5/536.pdf 传送门2: http://acm.gdufe.edu.cn/Problem/read/id/1 ...
- 内存池技术(UVa 122 Tree on the level)
内存池技术就是创建一个内存池,内存池中保存着可以使用的内存,可以使用数组的形式实现,然后创建一个空闲列表,开始时将内存池中所有内存放入空闲列表中,表示空闲列表中所有内存都可以使用,当不需要某一内存时, ...
- UVa 536 Tree Recovery(二叉树后序遍历)
Little Valentine liked playing with binary trees very much. Her favorite game was constructing rando ...
随机推荐
- 视频捕捉全教程(vc+vfw)
目 录 一. 视频捕获快速入门 二.基本的捕获设置 1.设置捕获速度: 2.设置终止捕获 3.捕获的时间限制 三.关于捕获窗口 1.创建一个AVICAP捕获窗口 2.将一个捕获窗口连接至捕获设备 3. ...
- zoj 3659 Conquer a New Region
// 给你一颗树 选一个点,从这个点出发到其它所有点的权值和最大// i 到 j的最大权值为 i到j所经历的树边容量的最小值// 第一感觉是树上的dp// 后面发现不可以// 看了题解说是并查集// ...
- poj 2063 Investment
题意:给定一个初始资金capital,然后给定d种投资方案,每种投资方案中有投资额value[i](是1000的倍数)和利息interest[i],每年的投资就可以拿到全部利息,然后累加起来继续投资利 ...
- 统计nginx日志里流量
用awk可以,比如,我想统计nginx日志里,今天下午3点0分,这一分钟内,访问的流量(文件的大小) grep "07/Nov/2013:15:00:" *.log|awk '{ ...
- 《C++ Primer 4th》读书笔记 第12章-类
原创文章,转载请注明出处:http://www.cnblogs.com/DayByDay/p/3936473.html
- SQL之50个常用的SQL语句
50个常用的sql语句 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,T ...
- 基于Fragment实现Tab的切换,滑出侧边栏
最近在学习Fragment(碎片)这是android3.0以后提出的概念,很多pad上面的设置部分都是通过Fragment来实现的,先看看具体的效果吧(图一) (图二) (图三)第一章图片是初始时的 ...
- POJ2987 Firing 最大权闭合图
详情请参考http://www.cnblogs.com/kane0526/archive/2013/04/05/3001557.html 值得注意的地方,割边会把图分成两部分,一部分和起点相连,另一部 ...
- 【windows核心编程】使用远程线程注入DLL
前言 该技术是指通过在[目标进程]中创建一个[远程线程]来达到注入的目的. 创建的[远程线程]函数为LoadLibrary, 线程函数的参数为DLL名字, 想要做的工作在DLL中编写. 示意图如下: ...
- hadoop测试环境主配置简例
1,mapred-site.xml 此配置文件主要是针对mapreduce的配置文件,配置的是jobtracker的地址和端口; <configuration> <property& ...