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 判断平衡二叉树的更多相关文章

  1. 数据结构实习 Problem H 迷宫的最短路径

    数据结构实习 Problem H 迷宫的最短路径 题目描述 设计一个算法找一条从迷宫入口到出口的最短路径. 输入 迷宫的行和列m n 迷宫的布局 输出 最短路径 样例输入 6 8 0 1 1 1 0 ...

  2. 数据结构实习 - Problem N 树的括号表示法

    writer:pprp date:20171103 题目描述 先将根结点放入一对圆括号中,然后把它的子树按由左而右的顺序放入括号中,而对子树也采用同样方法处理:同层子树与它的根结点用圆括号括起来,同层 ...

  3. 数据结构实习 problem O Huffman Tree

    Huffman Tree 题目描述 对输入的英文大写字母进行统计概率 然后构建哈夫曼树,输出是按照概率降序排序输出Huffman编码. 输入 大写字母个数 n 第一个字母 第二个字母 第三个字母 .. ...

  4. 数据结构实习 problem L 由二叉树的中序层序重建二叉树

    由二叉树的中序层序重建二叉树 writer:pprp 用层序中序来重建二叉树 代码点这里 其实本质上与前序中序建立二叉树没有什么太大区别 大概思路: 递归解法,对当前层进行处理,通过层序遍历可以得到当 ...

  5. 数据结构实习 - problem K 用前序中序建立二叉树并以层序遍历和后序遍历输出

    用前序中序建立二叉树并以层序遍历和后序遍历输出 writer:pprp 实现过程主要是通过递归,进行分解得到结果 代码如下: #include <iostream> #include &l ...

  6. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  7. 实验12:Problem D: 判断两个圆之间的关系

    Home Web Board ProblemSet Standing Status Statistics   Problem D: 判断两个圆之间的关系 Problem D: 判断两个圆之间的关系 T ...

  8. leetcode-110:判断平衡二叉树 Java

    Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a he ...

  9. 数据结构(六)查找---平衡二叉树(ASL)

    前提 我们之前的二叉排序树的插入(构建)是按照我们输入的数据来进行的,若是我们的数据分布不同,那么就会构造不同的二叉树 { , , , , , , , , , } { , , , , , , , , ...

随机推荐

  1. 微信js分享朋友圈(二)

    近期又用到微信分享的功能了.虽然不是第一次用了,依然我又有幸踩到了一个坑,所以分享一下吧. 根据微信sdk写的代码一步步很顺利,但是后面就是获取微信返回的分享结果的回调的时候IOS老是有问题,然后就网 ...

  2. Django logging模块

    一.Django logging配置 1.在setting.py中配置 # 日志文件存放路径 BASE_LOG_DIR = os.path.join(BASE_DIR, "log" ...

  3. Flume 1.7 源代码分析(四)从Source写数据到Channel

    Flume 1.7 源代码分析(一)源代码编译 Flume 1.7 源代码分析(二)总体架构 Flume 1.7 源代码分析(三)程序入口 Flume 1.7 源代码分析(四)从Source写数据到C ...

  4. neutron ml2

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhoumingbo532/article/details/27964675 在介绍ml2之前,先介绍 ...

  5. spring MVC学习(一)---前端控制器

    1.spring MVC中的前段控制器就是DsipatcherServlet,它在spring MVC框架中的结构图如下: 2.DispatcherServlet其实就是一个Servlet,它继承了H ...

  6. wpa安装方法

    1.openssl 2.lib 1.1.2 3.wpa lua 编译错误 http://www.blogjava.net/xiaomage234/archive/2013/09/13/404037.h ...

  7. POJ1836:Alignment(LIS的应用)

    题目链接:http://poj.org/problem?id=1836 题目要求: 给你n个数,判断最少去掉多少个数,从中间往左是递减的序列,往右是递增的序列 需注意的是中间可能为两个相同的值,如 1 ...

  8. PHP连接MYSQL操作数据库

    PHP连接MYSQL操作数据库 <?php $con = mysql_connect("localhost","root",""); ...

  9. AngularJS 笔记系列(五)过滤器 filter

    过滤器是用来格式化给用户展示的数据的. 在 HTML 中的模板绑定符号{{}} 内通过|符号来调用过滤器. 大写:{{ name | uppercase }} 也可以在 JS 中进行调用$filter ...

  10. linxu系统压缩解压命令

    使用cat命令进行文件的纵向合并 两种文件的纵向合并方法 归档文件和归档技术 归档的目的 什么是归档 tar命令的功能 tar命令的常用选项 使用tar命令创建.查看及抽取归档文件 使用tar命令创建 ...