这道题我的思路是先通过递归构建树,然后进行遍历将位置和保存在map映射中,最后按顺序输出map集合中的值。

至于如何遍历,我是依次尝试了宽度优先遍历和深度优先遍历,当然这都是可以的。不过期间写错了很多次。在此总结以下在这道题目中犯得错误(逻辑错误):

  1. 同一树,根节点地址不变,却多次使用
  2. 同一映射,不经清空却多次添加
  3. 遍历时在队列和栈中添加进的节点不是当前节点(即写错节点名称)
  4. 栈溢出导致结果出错
  5. 对一个节点分配内存只能在同一函数中进行 //否则需要添加指针引用(*指针也需要引用)

。。。。

以下附上我的AC代码:

  1. #include <cstdio>
  2. #include <queue>
  3. #include <iostream>
  4. #include <vector>
  5. #include <map>
  6. #include <stack>
  7. using namespace std;
  8. struct Node{
  9. Node *left;
  10. Node *right;
  11. int v;
  12. int k;
  13. Node():left(NULL),right(NULL){}
  14. };
  15. map<int,int> m;
  16. void remove_tree(Node* u){
  17. if(u==NULL)return ;
  18. remove_tree(u->left);
  19. remove_tree(u->right);
  20. delete u;
  21. }
  22. int solve(Node *& u){//特别注意这一段
  23. int v;
  24. scanf("%d", &v);
  25. if(v == -){ u->v=;return ;}
  26. else {
  27. u->left = new Node();
  28. u->right = new Node();
  29. u->v = v;
  30. solve(u->left);//左子树
  31. solve(u->right);//右子树
  32. }
  33. return ;
  34. }
  35. queue<Node*> q;
  36. //宽度优先遍历
  37. void bfs(Node * u){
  38. u->k=;
  39. q.push(u);
  40. while(!q.empty()){
  41. Node * node =q.front();
  42. q.pop();
  43. if(node->v)m[node->k] = m[node->k]+(node->v);
  44. if(node->left!=NULL&&node->left->v){node->left->k = (node->k)-;; q.push(node->left);}
  45. if(node->right!=NULL&&node->right->v){node->right->k = (node->k )+ ; q.push(node->right);}
  46. }
  47.  
  48. }
  49. stack<Node*> s;
  50. //深度优先遍历:stl栈板
  51. void dfs1(Node* u,int k){
  52.  
  53. u->k=k;
  54. s.push(u);
  55. while(!s.empty()){
  56. Node *node=s.top();
  57. s.pop();
  58. if(node->left!=NULL&&node->left->v){node->left->k=(node->k)-;s.push(node->left);}
  59. if(node->v)m[node->k] = m[node->k]+(node->v);
  60.  
  61. if(node->right!=NULL&&node->right->v){node->right->k=(node->k)+;s.push(node->right);}
  62.  
  63. }
  64. }
  65. //深度优先遍历:递归版
  66. void dfs(Node* u,int k){
  67. // printf("%d ,%d\n",u->v,k);
  68. if(u->v)m[k] = m[k]+(u->v);
  69. if(u->left!=NULL&&u->left->v)dfs(u->left,k-);
  70. if(u->right!=NULL&&u->right->v)dfs(u->right,k+);
  71. }
  72. int main(){
  73. #ifdef DEBUG
  74. freopen("6.10.in","r",stdin);
  75. freopen("6.10.out","w",stdout);
  76. #endif
  77. int n = ,r;
  78. Node *root=new Node() ;
  79. while((r=solve(root))){
  80. printf("Case %d:\n",++n);
  81. bfs(root);
  82.  
  83. //*****************注意在一个树使用完毕之后一定要删除它
  84. remove_tree(root);
  85. root=new Node();//注意要重新建一个根节点
  86.  
  87. int first=;
  88. for(map<int,int>::iterator it = m.begin(); it!= m.end(); ++it){
  89. if(!first)cout<<" ";else first=;
  90. cout <<it->second;
  91. }
  92. m.clear();//使用完之后要清空
  93.  
  94. printf("\n");
  95. if(r)printf("\n");
  96. else break;
  97. }
  98. return ;
  99. }

例题6-10 The Falling Leaves,UVA699的更多相关文章

  1. UVa699 The Falling Leaves

      // UVa699 The Falling Leaves // 题意:给一棵二叉树,每个节点都有一个水平位置:左儿子在它左边1个单位,右儿子在右边1个单位.从左向右输出每个水平位置的所有结点的权值 ...

  2. 【数据结构】The Falling Leaves(6-10)

    [UVA699]The Falling Leaves 算法入门经典第6章例题6-10(P159) 题目大意:有一颗二叉树,求水平位置的和. 试题分析:乱搞就可以过,将树根节点的pos记为0,向左-1, ...

  3. UVA 699 The Falling Leaves (二叉树水题)

    本文纯属原创.转载请注明出处,谢谢. http://blog.csdn.net/zip_fan. Description Each year, fall in the North Central re ...

  4. H - The Falling Leaves

    Description Each year, fall in the North Central region is accompanied by the brilliant colors of th ...

  5. UVA - 699The Falling Leaves(递归先序二叉树)

    The Falling Leaves Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Sub ...

  6. POJ 1577 Falling Leaves 二叉搜索树

    HDU 3791 Falling Leaves 二叉搜索树  Figure 1Figure 1 shows a graphical representation of a binary tree of ...

  7. UVA.699 The Falling Leaves (二叉树 思维题)

    UVA.699 The Falling Leaves (二叉树 思维题) 题意分析 理解题意花了好半天,其实就是求建完树后再一条竖线上的所有节点的权值之和,如果按照普通的建树然后在计算的方法,是不方便 ...

  8. The Falling Leaves(建树方法)

    uva 699 紫书P159 Each year, fall in the North Central region is accompanied by the brilliant colors of ...

  9. UVa 699 The Falling Leaves(递归建树)

    UVa 699 The Falling Leaves(递归建树) 假设一棵二叉树也会落叶  而且叶子只会垂直下落   每个节点保存的值为那个节点上的叶子数   求所有叶子全部下落后   地面从左到右每 ...

  10. UVa 699 The Falling Leaves (树水题)

    Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on ...

随机推荐

  1. struts (二)

    1.运行流程 client --->tomcat  ---> webapp  -->web.xml --->filter -->struts.xml ---->na ...

  2. mysql 如何用root 登录

    mysql -uroot -p 如果没有密码,按两下回车就进去了

  3. XXX项目 android 开发笔记

    1 工具? eclipse or android studio fragment 套用

  4. eclipse打jar包步骤

    eclipse->文件->export->java->JAR file 选择项目,Options增加Add directory entries finish hadoop ja ...

  5. SQL查询数据库是否存在

    在实际工作中会遇到通过SQL查询数据库是否存在的情况,下面一些语句可以提供一些帮助,本文的语句是在SQL08R2中测试的 1,查询当前数据库服务器所有数据库 select *  From master ...

  6. MyQL修改用户名命令、密码

    ====================================================== update user set host = '%' where user = 'root ...

  7. win10 TortoiseSVN 部分图标不显示

    原因:https://msdn.microsoft.com/en-us/library/cc144123(VS.85).aspx Note   The number of different icon ...

  8. OS X open finder here in terminal

    just type open .

  9. 欧几里得算法与扩展欧几里得算法_C++

    先感谢参考文献:http://www.cnblogs.com/frog112111/archive/2012/08/19/2646012.html 注:以下讨论的数均为整数 一.欧几里得算法(重点是证 ...

  10. 业务gis 搭建一个skyline 的js模板 (一)

    刚刚我们说的是二维的系统,如果要展示三维,我们是不是也需要这样,答案是必须的,是一定要,如果你是基于skyline做三维开发,业务开发人员要去搞那套api估计要吐血,所以我们必须得封装起来,这里不介绍 ...