数据结构实习 - 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)
前提 我们之前的二叉排序树的插入(构建)是按照我们输入的数据来进行的,若是我们的数据分布不同,那么就会构造不同的二叉树 { , , , , , , , , , } { , , , , , , , , ...
随机推荐
- d3.js:数据可视化利器之 selection:选择集
选择集/selection 选择集/selection是d3中的核心对象,用来封装一组从当前HTML文档中选中的元素: d3提供了两个方法用来创建selection对象: select(selecto ...
- MYSQL--表分区、查看分区(转)
一. mysql分区简介 数据库分区 数据库分区是一种物理数据库设计技术.虽然分区技术可以实现很多效果,但其主要目的是为了在特定的SQL操作中减少数据读写的总量以缩减sql语句的响应时间, ...
- 003-linux基本目录介绍
一.文件系统的类型 LINUX有四种基本文件系统类型:普通文件.目录文件.连接文件和特殊文件,可用file命令来识别. 普通文件:如文本文件.C语言元代码.SHELL脚本.二进制的可执行文件等,可用c ...
- nginx映射文件服务器文件夹
nginx映射文件服务器文件夹 普通用户A安装的nginx,yum源搭建文件服务器,新建普通用户B,其主目录是文件服务器需要访问的目录 普通用户A启动nginx无法访问B用户的文件服务器目录,提示40 ...
- JVM生命周期
JVM生命周期可以分为以下三个阶段 启动:任何class文件的main函数都可认为是jvm示例的起点. 运行:以main函数为起点,后续的线程都由它启动,包括守护线程和用户线程.main方法启动的线程 ...
- (转)全局变量、extern/static/const区别与联系
全局变量.extern/static/const区别与联系 编译单元(模块): 在IDE开发工具大行其道的今天,对于编译的一些概念很多人已经不再清楚了,很多程序员最怕的就是处理连接错误(LIN ...
- Visual studio插件 Reshaper--- 常用快捷键
快速修复 alt+enter (alt+enter唤出快速修复面板,上下方向键进行选择,再次敲击enter键确定修改) 跳转到对象声明 ctl+鼠标左键 重构-重命名(ctl+r+r) esc键退出 ...
- ASP.NET MVC BundleConfig介绍和使用
1.BundleConfig介绍: 在创建ASP.NET MVC5项目时,默认在App_Start文件夹中创建了BudleConfig.cs文件. public class BundleConfig ...
- Hive环境安装
说明: (Hbase依赖于Hadoop,同时需要把元数据存放在mysql中),mysql自行安装 Hadoop2.0安装参考我的博客: https://www.cnblogs.com/654wangz ...
- Ubuntu14.04+caffe+cuda7.5 环境搭建以及MNIST数据集的训练与测试
Ubuntu14.04+caffe+cuda 环境搭建以及MNIST数据集的训练与测试 一.ubuntu14.04的安装: ubuntu的安装是一件十分简单的事情,这里给出一个参考教程: http:/ ...