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 ...
随机推荐
- HDU 3749 Financial Crisis 经济危机(点双连通分量)
题意: 给一个图n个点m条边(不一定连通),接下来又q个询问,询问两个点是为“不相连”,“仅有一条路径可达”,“有两条及以上的不同路径可达”三种情况中的哪一种.注:两条以上的路径指的是路径上的点连1个 ...
- 分析一下FastDFS_java_client中TestClient.java这个文件以及跟它关联的这条线
本来先打算上个图来说明一下这条线的,可是我的画图工具还没有安装好,我先把跟TestClient.java相关的几个文件代码贴上来,但是由于代码行数还是不少的,所以请大家阅读文章的时候先不要展开代码,等 ...
- fzu 1675 The Seventy-seven Problem
给你长度为 10^5~10^6长度,由数字组成的串 其中有4位不见了 补全该串 使得在该串能整除 77的同时 尽可能大// 先计算出每个 n*10^m 模 77 的循环节 n=0,1,2..,9// ...
- addView的误区
如果在代码中动态使用addView(v),那么v里头所有在xml里设置好的layout_xxx全部失效!
- andeoid学习笔记七
Android中Broadcast的Intent大全 Api Level 3:(SDK 1.5) android.bluetooth.a2dp.intent.action.SINK_STATE_CHA ...
- [转] WinForm实现移除控件某个事件的方法
原文 WinForm实现移除控件某个事件的方法 本文实例讲述了WinForm实现移除控件某个事件的方法,供大家参考借鉴一下.具体功能代码如下: 主要功能部分代码如下: /// <summary& ...
- Selenium2Library系列 keywords 之 _SelectElementKeywords 之 get_selected_list_label(self, locator)
def get_selected_list_label(self, locator): """Returns the visible label of the selec ...
- 几种常见的FTP软件的二进制设置说明
几种常见的FTP软件的二进制设置说明: 1.FlashFXP: 打开 FlashFXP:在工具栏中,选项 => 参数(也可以直接按F6键),在弹出来的窗口中,选择“传输(T)”卡,在传输模式中选 ...
- 几个地图(高德、百度、Apple、Google)URL API(转)
转自:http://blog.csdn.net/cooldragon/article/details/20642131 移动应用中,如何在自己的App中调起第三方的原生地图App,并显示相关的信息,如 ...
- node-mysql中的连接池代码学习
node-mysql是一个node.js下的mysql驱动,前段时间在处理连接池的问题上遇到了连接不释放的疑难杂症,虽已解决,但仍需总结经验避免下次重蹈覆辙.下面是node-mysql中的连接池的部分 ...