1. /*
  2. 给出一棵满二叉树的先序遍历,有两种节点:字母节点(A-Z,无重复)和空节点(#)。要求这个树的中序遍历。输出中序遍历时不需要输出#。
  3. 满二叉树的层数n满足1<=n<=5。
  4.  
  5. Sample Input:
  6. ABC#D#E
  7.  
  8. Sample Output:
  9. CBADE
  10. */
  11. #include<cstdio>
  12. #include<cmath>
  13. #include<cstring>
  14. #include<iostream>
  15. using namespace std;
  16. const int M=;
  17. char data[M];
  18. int layer;
  19.  
  20. struct node
  21. {
  22. char data;
  23. struct node *l;
  24. struct node *r;
  25. };
  26.  
  27. void build(node * & t,int l)
  28. {
  29. if(l>layer)
  30. return ;
  31. int i=;
  32. while(data[i]=='\0')
  33. i++;
  34. char tc=data[i];
  35.  
  36. //cout<<i<<endl<<tc<<endl;
  37.  
  38. data[i]='\0';
  39. if(tc=='#')
  40. {
  41. t=NULL;
  42. return ;
  43. }
  44. t=new node();
  45. t->data=tc;
  46. build(t->l,l+);
  47. build(t->r,l+);
  48. }
  49.  
  50. void display(node *t)
  51. {
  52. if(t==NULL)
  53. return ;
  54. display(t->l);
  55. printf("%c",t->data);
  56. display(t->r);
  57. }
  58.  
  59. int main()
  60. {
  61. /*
  62. printf("%d\n",(int)(log(8)/log(2)));
  63. printf("%f\n",(log(7)/log(2)));
  64. */
  65. memset(data,'\0',sizeof(data));
  66. while(scanf("%s",&data))
  67. {
  68. //printf("%s\n",data);
  69. int n=strlen(data)+;
  70. /*
  71. if(log(n)/log(2)>(int)(log(n)/log(2)))
  72. layer=(int)(log(n)/log(2))+1;
  73. else*/
  74. layer=(int)(log(n)/log());
  75. //printf("%d\n",layer);
  76. node *tree;
  77. build(tree,);
  78. display(tree);
  79. }
  80.  
  81. return ;
  82. }

tz@HZAU

2019/3/13

【C++】满二叉树问题的更多相关文章

  1. UVA 712-S-Trees(满二叉树的简单查询)

    题意:给一棵满二叉树,叶子节点赋予权值,0或者1,对于每个查询输出叶子节点的权值,每个查询0代表往左走,1代表往右走,这题坑的地方是层的访问顺序,如第二组测试,由上到下依次是x3,x1,x2,假如给一 ...

  2. [Swift]LeetCode894. 所有可能的满二叉树 | All Possible Full Binary Trees

    A full binary tree is a binary tree where each node has exactly 0 or 2 children. Return a list of al ...

  3. python实现满二叉树递归循环

    一.二叉树介绍点这片文章 二叉树及题目介绍 例题: 有一颗满二叉树,每个节点是一个开关,初始全是关闭的,小球从顶点落下, 小球每次经过开关就会把它的状态置反,这个开关为关时,小球左跑,为开时右跑.现在 ...

  4. PAT甲级题解-1123. Is It a Complete AVL Tree (30)-AVL树+满二叉树

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6806292.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  5. 树&二叉树&&满二叉树&&完全二叉树&&完满二叉树

    目录 树 二叉树 完美二叉树(又名满二叉树)(Perfect Binary Tree) 完全二叉树(Complete Binary Tree) 完满二叉树(Full Binary Tree) 树 名称 ...

  6. 【algo&ds】【吐血整理】4.树和二叉树、完全二叉树、满二叉树、二叉查找树、平衡二叉树、堆、哈夫曼树、B树、字典树、红黑树、跳表、散列表

    本博客内容耗时4天整理,如果需要转载,请注明出处,谢谢. 1.树 1.1树的定义 在计算机科学中,树(英语:tree)是一种抽象数据类型(ADT)或是实作这种抽象数据类型的数据结构,用来模拟具有树状结 ...

  7. [LeetCode] 894. All Possible Full Binary Trees 所有可能的满二叉树

    A full binary tree is a binary tree where each node has exactly 0 or 2 children. Return a list of al ...

  8. 二叉树&满二叉树与完全二叉树

    二叉树的定义 二叉树(Binary Tree)是n(n≥0)个元素的有限集合,该集合为空或者为由一个称为"根"的元素及两个不相交的.被分别称为左子树和右子树的二叉树组成 二叉树的基 ...

  9. 找出 int 数组的平衡点 & 二叉树 / 平衡二叉树 / 满二叉树 / 完全二叉树 / 二叉查找树

    找出 int 数组的平衡点 左右两边和相等, 若存在返回平衡点的值(可能由多个); 若不存在返回 -1; ``java int [] arr = {2,3,4,2,4}; ```js const ar ...

随机推荐

  1. (转)Java代码书写规范

    0. 安装阿里代码规范的eclipse插件 https://www.cnblogs.com/caer/p/7753522.html 1.基本原则 强制性原则:     1.字符串的拼加操作,必须使用S ...

  2. torch分类问题

    import torch from torch.autograd import Variable import torch.nn.functional as F import matplotlib.p ...

  3. 3D Slicer中文教程(四)—图像分割

    1.数据获取 (1)下载3D Slicer自带的样本数据 (2)选择自由的数据 (3)网上数据库等其他方式下载数据 2.分割工具 Segment Editor是一个用于分割的模块.细分(也称为轮廓)描 ...

  4. 【原创】大数据基础之ElasticSearch(2)常用API整理

    Fortunately, Elasticsearch provides a very comprehensive and powerful REST API that you can use to i ...

  5. 【原创】运维基础之Docker(2)通过docker部署zookeeper nginx tomcat redis kibana/elasticsearch/logstash mysql kafka mesos/marathon

    通过docker可以从头开始构建集群,也可以将现有集群(配置以及数据)平滑的迁移到docker部署: 1 docker部署zookeeper # usermod -G docker zookeeper ...

  6. JS函数可以再添加属性(包括方法)

    1 前言 JS函数可以再添加属性(包括方法),这个有点有趣,记录一下. 2 代码 <!DOCTYPE html> <html> <head> <title&g ...

  7. CentOS7.6编译安装nginx

    配置阿里云yum源 cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak wget -O /etc/yu ...

  8. OrCAD Capture CIS 16.6 将版本16.6的设计文件另存为版本16.2的设计文件

    操作系统:Windows 10 x64 工具1:OrCAD Capture CIS 16.6-S062 (v16-6-112FF) 启动OrCAD Capture CIS,打开.dsn设计文件,右击该 ...

  9. centos查看系统/硬件信息及运维常用命令

    [root@yan-001 ~] # uname -a # 查看内核/操作系统/CPU信息的linux系统信息命令 [root@yan-001 ~] # head -n 1 /etc/issue # ...

  10. Vue中父子组件通讯——组件todolist

    一.todolist功能开发 <div id="root"> <div> <input type="text" v-model=& ...