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. 剑指Offer——按之字形顺序打印二叉树

    题目描述: 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推. 分析: 我们都知道二叉树的层次遍历用的是队 ...

  2. 搭建wordpress

    https://www.themepark.com.cn/xcjxgwordpressdzdyglyd.html

  3. LeetCode_Search in Rotated Sorted Array

    题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...

  4. appium+python自动化测试真机测试时报错“info: [debug] Error: Could not extract PIDs from ps output. PIDS: [], Procs: ["bad pid 'uiautomator'"]”

    刚开始启动服务时,弹出授权提示,以为是手机app权限问题,后来debug后,发现了一个警告日志:UiAutomator did not shut down fast enough, calling i ...

  5. Linux上安装rz和sz命令

    简介 lrzsz 官网入口:http://freecode.com/projects/lrzsz/ lrzsz是一个unix通信套件提供的X,Y,和ZModem文件传输协议 windows 需要向ce ...

  6. notepad插件:url变成可以点击的连接

  7. 微信对接HIS——微信可查检验结果

    患者仅仅要关注医院官方微信,不管身处何地,输入自己预留在医院的电话号码.检验单的条码号,就能够了解检验结果. 医院信息系统在提供病人数据信息前,会对查询方做身份认证和安全防护检測,录入患者挂号时预留的 ...

  8. python学习笔记(七)操作mysql

    python操作mysql数据库需要先安装pymysql模块,在之前博客中可翻看如何安装,最简单的就是pip install pymysql 1.导入pymysql模块   import pymysq ...

  9. javascript 中的 parameter vs arguments

    像往常一样简单粗暴地看码: A parameter is the variable which is part of the method’s signature (method declaratio ...

  10. C# 获取计算机cpu 硬盘 网卡信息

    /// <summary>/// 机器码         /// </summary>       public class MachineCode         {     ...