【C++】满二叉树问题
/*
给出一棵满二叉树的先序遍历,有两种节点:字母节点(A-Z,无重复)和空节点(#)。要求这个树的中序遍历。输出中序遍历时不需要输出#。
满二叉树的层数n满足1<=n<=5。 Sample Input:
ABC#D#E Sample Output:
CBADE
*/
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
using namespace std;
const int M=;
char data[M];
int layer; struct node
{
char data;
struct node *l;
struct node *r;
}; void build(node * & t,int l)
{
if(l>layer)
return ;
int i=;
while(data[i]=='\0')
i++;
char tc=data[i]; //cout<<i<<endl<<tc<<endl; data[i]='\0';
if(tc=='#')
{
t=NULL;
return ;
}
t=new node();
t->data=tc;
build(t->l,l+);
build(t->r,l+);
} void display(node *t)
{
if(t==NULL)
return ;
display(t->l);
printf("%c",t->data);
display(t->r);
} int main()
{
/*
printf("%d\n",(int)(log(8)/log(2)));
printf("%f\n",(log(7)/log(2)));
*/
memset(data,'\0',sizeof(data));
while(scanf("%s",&data))
{
//printf("%s\n",data);
int n=strlen(data)+;
/*
if(log(n)/log(2)>(int)(log(n)/log(2)))
layer=(int)(log(n)/log(2))+1;
else*/
layer=(int)(log(n)/log());
//printf("%d\n",layer);
node *tree;
build(tree,);
display(tree);
} return ;
}

tz@HZAU
2019/3/13
【C++】满二叉树问题的更多相关文章
- UVA 712-S-Trees(满二叉树的简单查询)
题意:给一棵满二叉树,叶子节点赋予权值,0或者1,对于每个查询输出叶子节点的权值,每个查询0代表往左走,1代表往右走,这题坑的地方是层的访问顺序,如第二组测试,由上到下依次是x3,x1,x2,假如给一 ...
- [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 ...
- python实现满二叉树递归循环
一.二叉树介绍点这片文章 二叉树及题目介绍 例题: 有一颗满二叉树,每个节点是一个开关,初始全是关闭的,小球从顶点落下, 小球每次经过开关就会把它的状态置反,这个开关为关时,小球左跑,为开时右跑.现在 ...
- PAT甲级题解-1123. Is It a Complete AVL Tree (30)-AVL树+满二叉树
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6806292.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 树&二叉树&&满二叉树&&完全二叉树&&完满二叉树
目录 树 二叉树 完美二叉树(又名满二叉树)(Perfect Binary Tree) 完全二叉树(Complete Binary Tree) 完满二叉树(Full Binary Tree) 树 名称 ...
- 【algo&ds】【吐血整理】4.树和二叉树、完全二叉树、满二叉树、二叉查找树、平衡二叉树、堆、哈夫曼树、B树、字典树、红黑树、跳表、散列表
本博客内容耗时4天整理,如果需要转载,请注明出处,谢谢. 1.树 1.1树的定义 在计算机科学中,树(英语:tree)是一种抽象数据类型(ADT)或是实作这种抽象数据类型的数据结构,用来模拟具有树状结 ...
- [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 ...
- 二叉树&满二叉树与完全二叉树
二叉树的定义 二叉树(Binary Tree)是n(n≥0)个元素的有限集合,该集合为空或者为由一个称为"根"的元素及两个不相交的.被分别称为左子树和右子树的二叉树组成 二叉树的基 ...
- 找出 int 数组的平衡点 & 二叉树 / 平衡二叉树 / 满二叉树 / 完全二叉树 / 二叉查找树
找出 int 数组的平衡点 左右两边和相等, 若存在返回平衡点的值(可能由多个); 若不存在返回 -1; ``java int [] arr = {2,3,4,2,4}; ```js const ar ...
随机推荐
- gzip 压缩
nginx 开启静态 gzip 配合 Vue 构建 在站点配置添加如下代码: location ~* \.(css|js)$ { gzip_static on; } 这是 nginx 的静态 gz ...
- 「luogu3258」[JLOI2014] 松鼠的新家
https://www.luogu.org/problemnew/show/P3258 (树剖裸题 树上差分 = = 差分 + lca 1. 树上差分基本思想:和差分一样,用前缀和的思想来处理解(操作 ...
- 【原创】大叔问题定位分享(7)Spark任务中Job进度卡住不动
Spark2.1.1 最近运行spark任务时会发现任务经常运行很久,具体job如下: Job Id ▾ Description Submitted Duration Stages: Succeed ...
- 03-django模型(1)
一.内容回顾 1.路由层 a.简单使用 b.有名分组 c.路由分发 d.反向解析 2.视图层 a.HttpRequest对象 常用的属性 常用方法 b.HttpResponse对象 响应三剑客 3.模 ...
- RC terms.
ETA: estimated time of arrival DEA: 1-Leg: 2-Leg: FCC: L10N: LocalizatioN i18N: InternationalizatioN ...
- Spring Boot 读取 resource 下文件
支持linux下读取 import org.springframework.core.io.ClassPathResource; public byte[] getCertStream(String ...
- uiautomator设备和选择器~Python详解
1.设备对象 引入uiautomator,获取设备对象<所谓设备对象可理解为:Android模拟器或者真机> 语法:from uiautomator import device as d ...
- Excel 转为 MySQL 语句
一.方法 一.假设你的表格有A.B.C三列数据,希望导入到你的数据库中表格table,对应的字段分别是col1.col2.col3 二.在你的表格中增加一列,利用excel的公式自动生成sql语句,具 ...
- 拷贝JAR包
package com.cici; import java.io.File; import java.io.FileInputStream; import java.util.ArrayList; i ...
- CentOS下安装yum源的流程和操作
一般公司都用Linux来搭建服务器,Linux安装软件时能够用yum安装依赖包是一件非常简单而幸福的事情,因为你只需一个简单的安装命令yum install []即可安装相应的软件,yum工具会自动的 ...