例题6-10 The Falling Leaves,UVA699
这道题我的思路是先通过递归构建树,然后进行遍历将位置和保存在map映射中,最后按顺序输出map集合中的值。
至于如何遍历,我是依次尝试了宽度优先遍历和深度优先遍历,当然这都是可以的。不过期间写错了很多次。在此总结以下在这道题目中犯得错误(逻辑错误):
- 同一树,根节点地址不变,却多次使用
- 同一映射,不经清空却多次添加
- 遍历时在队列和栈中添加进的节点不是当前节点(即写错节点名称)
- 栈溢出导致结果出错
- 对一个节点分配内存只能在同一函数中进行 //否则需要添加指针引用(*指针也需要引用)
。。。。
以下附上我的AC代码:
- #include <cstdio>
- #include <queue>
- #include <iostream>
- #include <vector>
- #include <map>
- #include <stack>
- using namespace std;
- struct Node{
- Node *left;
- Node *right;
- int v;
- int k;
- Node():left(NULL),right(NULL){}
- };
- map<int,int> m;
- void remove_tree(Node* u){
- if(u==NULL)return ;
- remove_tree(u->left);
- remove_tree(u->right);
- delete u;
- }
- int solve(Node *& u){//特别注意这一段
- int v;
- scanf("%d", &v);
- if(v == -){ u->v=;return ;}
- else {
- u->left = new Node();
- u->right = new Node();
- u->v = v;
- solve(u->left);//左子树
- solve(u->right);//右子树
- }
- return ;
- }
- queue<Node*> q;
- //宽度优先遍历
- void bfs(Node * u){
- u->k=;
- q.push(u);
- while(!q.empty()){
- Node * node =q.front();
- q.pop();
- if(node->v)m[node->k] = m[node->k]+(node->v);
- if(node->left!=NULL&&node->left->v){node->left->k = (node->k)-;; q.push(node->left);}
- if(node->right!=NULL&&node->right->v){node->right->k = (node->k )+ ; q.push(node->right);}
- }
- }
- stack<Node*> s;
- //深度优先遍历:stl栈板
- void dfs1(Node* u,int k){
- u->k=k;
- s.push(u);
- while(!s.empty()){
- Node *node=s.top();
- s.pop();
- if(node->left!=NULL&&node->left->v){node->left->k=(node->k)-;s.push(node->left);}
- if(node->v)m[node->k] = m[node->k]+(node->v);
- if(node->right!=NULL&&node->right->v){node->right->k=(node->k)+;s.push(node->right);}
- }
- }
- //深度优先遍历:递归版
- void dfs(Node* u,int k){
- // printf("%d ,%d\n",u->v,k);
- if(u->v)m[k] = m[k]+(u->v);
- if(u->left!=NULL&&u->left->v)dfs(u->left,k-);
- if(u->right!=NULL&&u->right->v)dfs(u->right,k+);
- }
- int main(){
- #ifdef DEBUG
- freopen("6.10.in","r",stdin);
- freopen("6.10.out","w",stdout);
- #endif
- int n = ,r;
- Node *root=new Node() ;
- while((r=solve(root))){
- printf("Case %d:\n",++n);
- bfs(root);
- //*****************注意在一个树使用完毕之后一定要删除它
- remove_tree(root);
- root=new Node();//注意要重新建一个根节点
- int first=;
- for(map<int,int>::iterator it = m.begin(); it!= m.end(); ++it){
- if(!first)cout<<" ";else first=;
- cout <<it->second;
- }
- m.clear();//使用完之后要清空
- printf("\n");
- if(r)printf("\n");
- else break;
- }
- return ;
- }
例题6-10 The Falling Leaves,UVA699的更多相关文章
- UVa699 The Falling Leaves
// UVa699 The Falling Leaves // 题意:给一棵二叉树,每个节点都有一个水平位置:左儿子在它左边1个单位,右儿子在右边1个单位.从左向右输出每个水平位置的所有结点的权值 ...
- 【数据结构】The Falling Leaves(6-10)
[UVA699]The Falling Leaves 算法入门经典第6章例题6-10(P159) 题目大意:有一颗二叉树,求水平位置的和. 试题分析:乱搞就可以过,将树根节点的pos记为0,向左-1, ...
- UVA 699 The Falling Leaves (二叉树水题)
本文纯属原创.转载请注明出处,谢谢. http://blog.csdn.net/zip_fan. Description Each year, fall in the North Central re ...
- H - The Falling Leaves
Description Each year, fall in the North Central region is accompanied by the brilliant colors of th ...
- UVA - 699The Falling Leaves(递归先序二叉树)
The Falling Leaves Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Sub ...
- POJ 1577 Falling Leaves 二叉搜索树
HDU 3791 Falling Leaves 二叉搜索树 Figure 1Figure 1 shows a graphical representation of a binary tree of ...
- UVA.699 The Falling Leaves (二叉树 思维题)
UVA.699 The Falling Leaves (二叉树 思维题) 题意分析 理解题意花了好半天,其实就是求建完树后再一条竖线上的所有节点的权值之和,如果按照普通的建树然后在计算的方法,是不方便 ...
- The Falling Leaves(建树方法)
uva 699 紫书P159 Each year, fall in the North Central region is accompanied by the brilliant colors of ...
- UVa 699 The Falling Leaves(递归建树)
UVa 699 The Falling Leaves(递归建树) 假设一棵二叉树也会落叶 而且叶子只会垂直下落 每个节点保存的值为那个节点上的叶子数 求所有叶子全部下落后 地面从左到右每 ...
- UVa 699 The Falling Leaves (树水题)
Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on ...
随机推荐
- C++学习12 友元函数和友元类
友元函数和友元类在实际开发中较少使用,想快速学习C++的读者可以跳过本节. 一个类中可以有 public.protected.private 三种属性的成员,通过对象可以访问 public 成员,只有 ...
- Eclipse自动补全功能和自动生成作者、日期注释等功能设置
修改作者.日期注释格式:打开Windows->Preferences->Java->Code Style->Code Templates,点击右边窗口中的Comments,可以 ...
- [kuangbin带你飞]专题十四 数论基础
ID Origin Title 111 / 423 Problem A LightOJ 1370 Bi-shoe and Phi-shoe 21 / 74 Problem B ...
- flash 读取系统默认编码
java有类可以直接读取,但貌似flash没有. Charset.defaultCharset(); 但是浏览器里可以有. document.defaultCharset;//从当前的区域语言中获取默 ...
- HDU 4405 【概率dp】
题意: 飞行棋,从0出发要求到n或者大于n的步数的期望.每一步可以投一下筛子,前进相应的步数,筛子是常见的6面筛子. 但是有些地方可以从a飞到大于a的b,并且保证每个a只能对应一个b,而且可以连续飞, ...
- Android中 View not attached to window manager错误的解决办法
前几日出现这样一个Bug是一个RuntimeException,详细信息是这样子的:java.lang.IllegalArgumentException: View not attached to w ...
- OC基础(2)
类与对象 类的设计 第一个OC类 对象方法的声明和实现 类方法的声明和实现 *:first-child { margin-top: 0 !important; } body > *:last-c ...
- [原创]大连sap vt 实习生面试经历
从决定参加这次面试开始,求职生涯就算是开始了,虽然失败了,但也是亲身体会到了面试的感觉,这次经历也作为第一篇博客. 大连sap的职能是sap的全球技术支持中心和解决方案提供中心.VT项目提供的岗位是技 ...
- Flash视频播放器开发经验总结
HTTP协议更优 目前几乎所有的视频点播网站全部采用HTTP协议传输数据.因为相对于诸如RTMP等协议来说,HTTP协议是无状态的,数据传输完毕就断开连接,这样服务器就可以腾出资源来服务更多的用户.而 ...
- c# 图片路径转byte[] 插到数据库BLOB 图片长宽自定义
//根据图片路径读取图片并且转byte[] 类型 FileStream fs = new FileStream(filePath, FileMode.Open); byte[] byData = ...