题意:

计算从根到叶节点的累加值,看看是否等于指定值。是输出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的更多相关文章

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

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

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

    Tree Summing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8132   Accepted: 1949 Desc ...

  3. UVA.548 Tree(二叉树 DFS)

    UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...

  4. POJ 1145 Tree Summing

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

  5. 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 ...

  6. 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 ...

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

  8. 内存池技术(UVa 122 Tree on the level)

    内存池技术就是创建一个内存池,内存池中保存着可以使用的内存,可以使用数组的形式实现,然后创建一个空闲列表,开始时将内存池中所有内存放入空闲列表中,表示空闲列表中所有内存都可以使用,当不需要某一内存时, ...

  9. UVa 536 Tree Recovery(二叉树后序遍历)

    Little Valentine liked playing with binary trees very much. Her favorite game was constructing rando ...

随机推荐

  1. UVA 550 Multiplying by Rotation (简单递推)

    题意:有些数字是可以这样的:abcd*k=dabc,例如179487 * 4 = 717948,仅仅将尾数7移动到前面,其他都不用改变位置及大小.这里会给出3个数字b.d.k,分别代表b进制.尾数.第 ...

  2. liux之我用过的zip解压命令

    用途说明 zip文件是一种常用的压缩文件格式,WinZip.WinRar等压缩软件都支持zip文件格式,就连java的jar包也是zip格式 的,Firefox插件xpi文件也是zip格式的.Linu ...

  3. ubuntun安装ssh,并远程链接服务器操作

    SSH是一种以安全.加密方式连接远程主机或服务器的方法.SSH服务器接受从有SSH的客户机的连接,允许操作者象在本地一样地登录系统.你可以用SSH从远程运行shell和X程序. (1)安装SSH服务器 ...

  4. Xcode中使用svn时,报证书验证错误Error validating server certificate for

    转:http://blog.csdn.net/yhawaii/article/details/7511141 今天使用Xcode自带的svn客户端时,总是连接不上服务器,报如下错误: Error va ...

  5. Canvas处理头像上传

    未分类 最近社区系统需要支持移动端,其中涉及到用户头像上传,头像有大中小三种尺寸,在PC端,社区用Flash来处理头像编辑和生成,但该Flash控件的界面不友好而且移动端对Flash的支持不好,考虑到 ...

  6. Gridview LookupEdit gridLookUpEdit z

    LookupEdit一般用法: 绑定数据源:                                 lookUpEdit.Properties.ValueMember = 实际要用的字段;  ...

  7. HDU 5001-Walk(概率dp)

    题意: 给你一个图,求在长度为d的所有路径,不经过每个结点的概率 分析: 枚举每个结点,正推求概率 #include <map> #include <set> #include ...

  8. 一行命令实现Android自动关机

    前几天晚上失眠,实在睡不着觉,于是想用Nexus7听一听小野丽莎的歌,在安静祥和之中睡去(怎么感觉有点...)但是不能让平板总是这么循环播放吧(屋里吐槽Google Play Music),所以在平板 ...

  9. HDU 1890 Robotic Sort(splay)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1890 [题意] 给定一个序列,每次将i..P[i]反转,然后输出P[i],P[i]定义为当前数字i ...

  10. Mysql视图的作用及其性能分析

    定义:视图是从一个或几个基本表导出的表,它与基本表不同,是一个虚表. 作用: 1.简化操作,不用进行多表查询. 2.当不同种类的用用户共享同一个数据库时,非常灵活,(用户以不同的 方式看待同一数据. ...