Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

题意:二叉树中以任意节点为父结点的两棵子树的深度差不超过1,为平衡二叉树。

个人思路变换过程,课忽略刚开始,理解为完全二叉树了,即任意两层的深度差不超过1,想到用层次遍历,找到第一个没有左右孩子的结点所在的层,然后继续遍历,只要层差超过1就返回false,结果被打脸。例 {1,#,2,#,3}不是平衡二叉树,但就我最初的想法是符合,所以思路不对 。后来又想到,左、右孩子中只要有一个为NULL就为最小层,然后和整个层数去比,结果又无情的被打脸。例:{1,2,2,3,3,3,3,4,4,4,4,4,4,#,#,5,5},其次我对最小层的理解也是不对的。究其所有,是没有理解平衡二叉树的概念。是任意结点的两子树的深度不超过1

特别值得注意的是,一棵子树的深度指的是最大深度,两棵子树的深度差应为两者的最大深度之差。平衡二叉树详见。整体的思路:找到以每个结点为根结点的左右子树深度是否相差不大于1,从而判断是否是平衡的。

所以,参考大神们的解法:

递归大法。寻找最大深度的函数,可以使用层次遍历和最大深度中提及的方法。

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isBalanced(TreeNode *root)
{
if(root==NULL) return true;
if(abs(getDepth(root->left)-getDepth(root->right))>)
return false;
return isBalanced(root->left)&&isBalanced(root->right);
} int getDepth(TreeNode *root)
{
if(root==NULL) return ;
return +max(getDepth(root->left),getDepth(root->right));
}
};

改进,来源LeetCode。改进的思路:上面的算法,需要计算深度时,每个结点都要计算一遍。这样就会影响效率,若是发现子树不平衡,则不计算具体的深度,而是直接返回-1。优化后,对每个结点获得左右子树的深度后,若是,平衡的返回真实深度,不然就返回-1.

 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isBalanced(TreeNode *root)
{
if(root==NULL) return true;
int l=getHeight(root->left);
int r=getHeight(root->right); if(l<||r<||abs(l-r)>) return false;
return true;
} int getHeight(TreeNode *root)
{
if(root==NULL) return ;
int l=getHeight(root->left);
int r=getHeight(root->right); if(l<||r<||abs(l-r)>) return -; //表示不平衡的情况
return max(l,r)+;
}
};

[Leetcode] Balanced binary tree平衡二叉树的更多相关文章

  1. [LeetCode] Balanced Binary Tree 平衡二叉树

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

  2. [CareerCup] 4.1 Balanced Binary Tree 平衡二叉树

    4.1 Implement a function to check if a binary tree is balanced. For the purposes of this question, a ...

  3. LeetCode: Balanced Binary Tree 解题报告

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

  4. [LeetCode] 110. Balanced Binary Tree 平衡二叉树

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

  5. LeetCode——Balanced Binary Tree(判断是否平衡二叉树)

    问题: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...

  6. LeetCode之Balanced Binary Tree 平衡二叉树

    判定一棵二叉树是不是二叉平衡树. 链接:https://oj.leetcode.com/problems/balanced-binary-tree/ 题目描述: Given a binary tree ...

  7. 【LeetCode】Balanced Binary Tree(平衡二叉树)

    这道题是LeetCode里的第110道题. 题目要求: 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. ...

  8. LeetCode 110. Balanced Binary Tree平衡二叉树 (C++)

    题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...

  9. [leetcode]Balanced Binary Tree @ Python

    原题地址:http://oj.leetcode.com/problems/balanced-binary-tree/ 题意:判断一颗二叉树是否是平衡二叉树. 解题思路:在这道题里,平衡二叉树的定义是二 ...

随机推荐

  1. PHP队列之理论篇

    定义: 特殊的线性表.       特点: 1.先进先出:连结性. 2.作为一种特殊性的表,主要是在表前端进行删除操作,我们称删除的端为对头(front):只能在表的后端进行插入操作,我们称之为称插入 ...

  2. djangorestframework怎么这么好用!

    一年前就已经用过restframework, 当时觉得这个只是给web框架打辅助的, 他能实现的我也都实现(可能没有那么好用, 嘿嘿) 但是我有一种东西叫做效率, 时间就是金钱, 别人造好的就直接用就 ...

  3. thinkphp 3.2中依靠关联模型来关联三个表

    这里说的是用thinkphp3.2关联模型关联三个表 根据用户表查询出三个表的数据,需要两个model来配合,第一个model是根据user表来查询到班级的信息,然后第二个model是根绝banji中 ...

  4. 第一个网页(仿照当当网,仅仅使用CSS)

    这个网页是在学过CSS之后,对当当网首页进行模仿的网页,没有看当当网的网页源码,纯按照自己之前学的写的,由于是刚学过HTML和CSS才一个星期,所以里面有许多地方写的非常没有水平,仅仅用来学习使用,欢 ...

  5. [转]Makefile中使用$$的使用

    在makefile中,会经常使用shell命令,也经常见到$var 和 $$var的情况,有什么区别呢,区别大了.不要认为在makefile的规则的命令行中使用$var就是将makefile的变量和s ...

  6. spring配置jackson不返回null值

    #json不返回null spring.jackson.default-property-inclusion=non_null

  7. python2.7入门---循环语句(while)

        接下来就要了解循环语句了.我们都知道,程序在一般情况下是按顺序执行的.编程语言提供了各种控制结构,允许更复杂的执行路径.循环语句允许我们执行一个语句或语句组多次,下面是在大多数编程语言中的循环 ...

  8. PHP.43-TP框架商城应用实例-后台18-商品属性3-库存量管理

    库存量管理 思想:为商品的每个多选属性设置库存量!!要把多选属性排列组合分别指定库存量!! 效果如下:[由商品已经添加的属性决定] 1.建表goods_number{goods_id,goods_nu ...

  9. RelativeSource设定绑定方向

    <Window x:Class="Yingbao.Chapter2.RelativeEx.AppWin" xmlns="http://schemas.microso ...

  10. XStream轻松转换xml和java对象

    首先引入所需的jar: xstream-1.4.9.xpp3_min-1.1.4c.dom4j-1.6.1, 或用maven管理jar包时在pom.xml中添加: <!-- https://mv ...