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. ovn-kubernetes执行流程概述

    Master部分 1.master初始化 以node name创建一个distributed logical router 创建两个load balancer用于处理east-west traffic ...

  2. 【Unity Shader编程】之十六 基于MatCap实现适于移动平台的“次时代”车漆Shader

    本系列文章由@浅墨_毛星云 出品,转载请注明出处.   文章链接:http://blog.csdn.net/poem_qianmo/article/details/55803629 渲染本文配图使用的 ...

  3. cordova 入门

    1. npm install -g cordova On Windows, npm can usually be found at C:\Users\username\AppData\Roaming\ ...

  4. 过程记录:搭建wordpress站点

    过程记录:搭建wordpress站点 前提:现在aws中搭建好LNAMP环境和网络mysql数据库,即为下载的wdcp和aws的rds 1.获取WordPress安装包(中文版) https://cn ...

  5. 008-Shell 流程控制

    一.if else 1.1.if if 语句语法格式: if condition then command1 command2 ... commandN fi 写成一行(适用于终端命令提示符): ]; ...

  6. 一种SPA(单页面应用)架构

    (如果对SPA概念不清楚的同学可以先自行了解相关概念) 平时喜欢做点小页面来玩玩,并且一直采用单页面应用(Single Page Application)的方式来进行开发.这种开发方式是在之前一年做的 ...

  7. Linux系统下RPM命令和yum的使用

    Linux系统下RPM命令和yum的使用 RPM:Redhat Packages Manager (红帽系列软件包的管理),主要用于安装.卸载.升级和管理软件. 一个包由下面几个部分构成: 例如:ht ...

  8. robotFramework_ride_python2_Wxpython测试环境搭建

    (提示:我的安装版本是robotFramework3.0+ride1.5+python2.7+wxpython2.8,至于wxpython3.0下ride安装打不开的问题我还没找到原因,建议刚开始先不 ...

  9. 关于Windows Service的一个编写技巧

    写过Windows Service的朋友都知道服务是不可以直接在vs里面启动调试,我们必须修改Program.cs文件来达到我们调试的目的,等服务调试好了以后还要把代码改回来,显非常的不方便,在这里为 ...

  10. PHP下使用Redis消息队列发布微博(复制)

    phpRedisAdmin :github地址  图形化管理界面 git clone https://github.com/ErikDubbelboer/phpRedisAdmin.git cd ph ...