题意:

计算从根到叶节点的累加值,看看是否等于指定值。是输出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. eclipse 下生成jar包

    eclipse 下生成jar包 第一:普通类导出jar包,我说的普通类就是指此类包含main方法,并且没有用到别的jar包. 1.在eclipse中选择你要导出的类或者package,右击,选择Exp ...

  2. MySQL操作备忘录

    在mysql包中,mysqld是数据库服务器,mysql是客户端,mysqladmin则用于管理数据库服务器的信息,如用户密码等. 关于安装: 1.在d:/sftwr/mysql/bin目录下: my ...

  3. group by的使用

    1.概述 2.原始表 3.简单Group By 4.Group By 和 Order By 5.Group By中Select指定的字段限制 6.Group By All 7.Group By与聚合函 ...

  4. 底部菜单栏(二) TabHost & RadioGroup 实现

    需求:使用TabHost & RadioGroup实现底部菜单栏: 效果图: 实现分析: 1.目录结构: 代码实现: 1. activity_main.xml <?xml version ...

  5. Delphi RICHEDIT中插入图象

    unit InsRich;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  6. HDU 5776 sum (BestCoder Round #85 A) 简单前缀判断+水题

    分析:就是判断简单的前缀有没有相同,注意下自身是m的倍数,以及vis[0]=true; #include <cstdio> #include <cstdlib> #includ ...

  7. FOJ2022车站 线段树区间合并

    http://acm.fzu.edu.cn/problem.php?pid=2022 刚开始MLE,用map对应,果断爆内存了,然后改用去重,离散化, lowbound查找元素位置,速度还不错,不过p ...

  8. xcode 怎么样在发布release版本的时候 不输出log

    我们平时在开发应用的时候,经常会用到 NSLog 来调试我们的程序,而随着项目越来越大,这些用于调试的日志输出就会变得很难管理. 发布正式版的时候一定要屏蔽掉所有后台输出,因为这些输出还是比较消耗系统 ...

  9. VmWare下安装Linux Centos6.0详细过程

    http://www.linuxidc.com/Linux/2012-12/76583.htm和http://mirrors.163.com/centos/6.5/isos/i386/这是有关VmWa ...

  10. 【跟我一起学Python吧】python with statement 进阶理解

    由于之前有一个项目老是要打开文件,然后用pickle.load(file),再处理...最后要关闭文件,所以觉得有点繁琐,代码也不简洁.所以向python with statement寻求解决方法.以 ...