数据结构实习 - problem M 判断平衡二叉树
writer:pprp
date: 20171103
题目描述
给定一棵二叉树的中序和层序输出,判断是否为平衡二叉树的。如果是,输出YES如果不是输出NO。
输入
树结点个数
中序遍历序列
层序遍历序列
输出
是否是平衡二叉树的判断结论
样例输入
样例1:
3
1 2 3
2 1 3
样例2:
4
1 2 3 4
1 2 3 4
样例输出
样例1:
YES
样例2:
NO
代码如下:
//M: 判别平衡二叉树
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int maxn = 1000;
int n;
struct tree
{
tree * lchild;
tree * rchild;
int val;
tree():lchild(NULL),rchild(NULL),val(0) {}
};
tree* createTree(int * layer, int * in,int n)
{
if(n == 0)
return NULL;
int rin[maxn],lin[maxn],rlayer[maxn],llayer[maxn];
memset(rin,0,sizeof(rin)),memset(lin,0,sizeof(lin));
memset(rlayer,0,sizeof(rlayer)),memset(llayer,0,sizeof(llayer));
tree * node = new tree;
node->val = layer[0];
int index = 0;
for(index = 0 ; index < n ; index++)
if(in[index] == layer[0])
break;
if(index == n)
{
cout << "Not found 404" << endl;
return NULL;
}
int cnt = 0;
for(int i = 0; i < index; i++)
lin[cnt++] = in[i];
cnt = 0;
for(int i = index+1 ; i < n; i++)
rin[cnt++] = in[i];
int llayercnt = 0, rlayercnt = 0;
for(int i = 1 ; i < n; i++)
{
for(int j = 0 ; j < index; j++)
{
if(lin[j] == layer[i])
llayer[llayercnt++] = layer[i];
}
}
for(int i = 1; i < n ; i++)
{
for(int j = 0 ; j < cnt; j++)
{
if(rin[j] == layer[i])
rlayer[rlayercnt++] = layer[i];
}
}
node->lchild = createTree(llayer,lin,llayercnt);
node->rchild = createTree(rlayer,rin,rlayercnt);
return node;
}
bool solve(tree * root, int& depth)
{
if(root == NULL)
{
depth = 0;
return true;
}
int leftdepth, rightdepth;
bool bleft = solve(root->lchild,leftdepth);
bool bright = solve(root->rchild,rightdepth);
if(bleft && bright)
{
if(abs(leftdepth-rightdepth)<=1)
{
depth = 1+(leftdepth>rightdepth?leftdepth:rightdepth);
return true;
}
}
return false;
}
void post(tree * root)
{
if(root != NULL)
{
post(root->lchild);
post(root->rchild);
cout << root->val << " ";
}
}
int main()
{
int n;
cin >> n;
int layer[maxn],in[maxn];
for(int i = 0 ; i < n; i++)
cin >> in[i];
for(int j = 0 ; j < n ; j++)
cin >> layer[j];
tree * root = createTree(layer,in,n);
int depth = 0;
// post(root);
// cout << endl;
if(solve(root,depth))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
数据结构实习 - problem M 判断平衡二叉树的更多相关文章
- 数据结构实习 Problem H 迷宫的最短路径
数据结构实习 Problem H 迷宫的最短路径 题目描述 设计一个算法找一条从迷宫入口到出口的最短路径. 输入 迷宫的行和列m n 迷宫的布局 输出 最短路径 样例输入 6 8 0 1 1 1 0 ...
- 数据结构实习 - Problem N 树的括号表示法
writer:pprp date:20171103 题目描述 先将根结点放入一对圆括号中,然后把它的子树按由左而右的顺序放入括号中,而对子树也采用同样方法处理:同层子树与它的根结点用圆括号括起来,同层 ...
- 数据结构实习 problem O Huffman Tree
Huffman Tree 题目描述 对输入的英文大写字母进行统计概率 然后构建哈夫曼树,输出是按照概率降序排序输出Huffman编码. 输入 大写字母个数 n 第一个字母 第二个字母 第三个字母 .. ...
- 数据结构实习 problem L 由二叉树的中序层序重建二叉树
由二叉树的中序层序重建二叉树 writer:pprp 用层序中序来重建二叉树 代码点这里 其实本质上与前序中序建立二叉树没有什么太大区别 大概思路: 递归解法,对当前层进行处理,通过层序遍历可以得到当 ...
- 数据结构实习 - problem K 用前序中序建立二叉树并以层序遍历和后序遍历输出
用前序中序建立二叉树并以层序遍历和后序遍历输出 writer:pprp 实现过程主要是通过递归,进行分解得到结果 代码如下: #include <iostream> #include &l ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- 实验12:Problem D: 判断两个圆之间的关系
Home Web Board ProblemSet Standing Status Statistics Problem D: 判断两个圆之间的关系 Problem D: 判断两个圆之间的关系 T ...
- leetcode-110:判断平衡二叉树 Java
Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a he ...
- 数据结构(六)查找---平衡二叉树(ASL)
前提 我们之前的二叉排序树的插入(构建)是按照我们输入的数据来进行的,若是我们的数据分布不同,那么就会构造不同的二叉树 { , , , , , , , , , } { , , , , , , , , ...
随机推荐
- html中a标签的target属性
_blank -- 在新窗口中打开链接 _parent -- 在父窗体中打开链接 _self -- 在当前窗体打开链接,此为默认值 _top -- 在当前窗体打开链接,并替换当前的整个窗体(框架页) ...
- ubuntu开机自动加载iptables配置(转)
原文:http://www.xuebuyuan.com/730127.html iptables的使用参见http://wiki.ubuntu.org.cn/IptablesHowTo iptable ...
- 《Monitoring and Tuning the Linux Networking Stack: Receiving Data》翻译
Overview 从宏观的角度来看,一个packet从网卡到socket接收缓冲区的路径如下所示: 驱动加载并初始化 packet到达网卡 packet通过DMA被拷贝到内核中的一个ring buff ...
- Shiro框架简介
Apache Shiro是Java的一个安全框架.对比另一个安全框架Spring Sercurity,它更简单和灵活. Shiro可以帮助我们完成:认证.授权.加密.会话管理.Web集成.缓存等. A ...
- (2.4)备份与还原--WAL与备份原理
预写式日志(Write-Ahead Logging (WAL)) 部分转自:http://www.cnblogs.com/wenBlog/p/4423497.html SQL Server中使用了W ...
- 5.如何调节ubuntu的分辨率
http://jingyan.baidu.com/article/0964eca2351ed58285f5361d.html
- HTTP中的header头解析说明
HTTP的头信息比较多,这里根据实际例子作出说明.下面图片是访问 http://kccdzz.com 的一个HTTP请求的header信息,可以看出Headers主要分为Response Header ...
- strtoul函数的使用
函数原型: unsigned long strtoul(const char *nptr,char **endptr,int base ) 参数1:字符串起始地址参数2:返回字符串有效数字的结束地址, ...
- Java 7 新增功能
Java 7 新增功能如下: 对二进制整数的支持,以0b或0B开头. 在数值中可以使用下划线,不管是整型数值,还是浮点型数值,都可以自由地使用下划线,这样可以直观地分辨数值常量中到底包含多少位.如:3 ...
- SQL Server DDL触发器
DDL 触发器作用: DDL 触发器主要用于防止对数据库架构.视图.表.存储过程等进行的某些修改. DDL 触发器事件: DDL 触发器在创建用来监视并响应该数据库或服务器实例中的活动的事件通知时,可 ...