题意:

计算从根到叶节点的累加值,看看是否等于指定值。是输出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. ZOJ1260/POJ1364国王(King)

    // 题意 问是否存在一个长度为n的序列// 这个序列满足m个限制// 每个限制有 si ni oi kisi 为序列位置 ni为从si开始连续长度为ni+1 的子序列 这些子序列和 大于或小于 ki ...

  2. java 访问器方法中对象引用的问题

    "注意不要编写返回引用可变对象的访问器方法".因为会破坏类的封装性,引用的内容可能会被改变,产生业务逻辑上的错误. 什么是可变对象? 先要搞清楚java中值传递和引用传递的问题,总结如下: 1.对象就 ...

  3. Android 如何使用juv-rtmp-client.jar向Red5服务器发布实时视频数据

    使用juv-client-client.jar主要是尽快地完成毕业设计里面手机端向网页端发送实时视频的功能,由于实习和做毕业设计的时间冲突,因此完成毕业设计只花了1个多月时间. (万恶的形式主义,论文 ...

  4. [转] AE之分级颜色专题图渲染

    原文 AE之分级颜色专题图渲染 参考代码1 private void 分级渲染ToolStripMenuItem_Click(object sender, EventArgs e) { //值分级 I ...

  5. Win10遇上Kindle就蓝屏

    在使用 Kindle 连接 Win10 时会出现蓝屏现象,现在,微软承认 Windows 10 插入 Kindle 导致蓝屏问题,并表示目前正着手制作补丁.微软表示:“我们承认确实存在当Kindle ...

  6. Redis常用命令手册:服务器相关命令

    Redis提供了丰富的命令(command)对数据库和各种数据类型进行操作,这些command可以在Linux终端使用.在编程时,比如各类语言包,这些命令都有对应的方法.下面将Redis提供的命令做一 ...

  7. SQL SERVER 实现分组合并实现列数据拼接

    需求场景: SQL SERVER 中组织的数据结构是一个层级关系,现在需要抓出每个组织节点以上的全部组织信息,数据示例如下: ADOrg_ID--------------ParentID------- ...

  8. 如何给10^7个数据量的磁盘文件排序--bitset

    题目: 输入:给定一个文件,里面最多含有n个不重复的正整数(也就是说可能含有少于n个不重复正整数),且其中每个数都小于等于n,n=10^7.输出:得到按从小到大升序排列的包含所有输入的整数的列表. 分 ...

  9. android判断当前网络状态及跳转到设置界面

    今天,想做这个跳转到网络设置界面, 刚开始用 intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS); 不料老是出现settings.Wirele ...

  10. <Stackoverflow> 声望和节制

    什么是声望(reputation)?我是怎样获得(或失去)它的? 声望是一种粗略的测量,用来表示社区对你的信任度.通过让别人相信你知道自己正在讨论什么来获得.对网站的基本使用,包括问一个问题,回答,建 ...