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. python的基本知识,range在python2.x中和python3.x中的区别

    这些是最开始学习python时的笔记,今天整理一下,在这里记录一下. 各种基础代码解释 for key,item in enumerate(li): print(key,item) inp=input ...

  2. QOS-Qos标记和QOS-Policy策略

    QOS-Qos标记和qos  policy策略 2018年7月7日 20:29 主要标记方法 : IP ToS字段标记 IP Precedence(IP优先级) DSCP 二层 802.1p  CoS ...

  3. 密码发生器 南阳acm519

    密码发生器 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 在对银行账户等重要权限设置密码的时候,我们常常遇到这样的烦恼:如果为了好记用生日吧,容易被破解,不安全:如 ...

  4. Preparing Cities for Robot Cars【城市准备迎接自动驾驶汽车】

    Preparing Cities for Robot Cars The possibility of self-driving robot cars has often seemed like a f ...

  5. python入门——Anaconda安装

    初学Python,可以选择python原始的IDE,但原始的IDE在使用过程中需要自己安装各种包,个人觉得初学者不需要将时间花在这些上面,而是应该直接学习python程序,这些比较杂的事情可以在以后的 ...

  6. HyperLedger Fabric 1.4 超级账本组织(5.3)

    超级账本组织分为TSC(技术指导委员会).Governing Board(董事会成员).LF Staffs(工作人员)三个组织,组织架构图如下: TSC:技术指导委会员,主导社区的开发工作,下设多个工 ...

  7. AES128加密-S盒和逆S盒构造推导及代码实现

    文档引用了<密码编码学与网络安全--原理和实践>里边的推导过程,如有不妥,请与我联系修改. 文档<FIPS 197>高级加密标准AES,里边有个S盒构造,涉及到了数论和有限域的 ...

  8. DDL失败案例

    问题描述 今天对线上某个业务的大表120G进行重建表操作时遇到报错,该表有个比较显著的特征是*写入量比较大,每天写入加更新的频率在数千万级别.大致的环境 1 版本:Percona 5.6.24 2 操 ...

  9. MVC使用ajax取得JSon数据

    为了在view中获取模型中的数据,用ajax异步模式读取数据,再用json返回的view中. 1.controller中: [HttpPost] public ActionResult GetAjax ...

  10. 【jQuery】 效果

    [jQuery] 效果 资料 http://www.w3school.com.cn/jquery/jquery_ref_effects.asp 1. 显示隐藏 hide(); 隐藏 show(): 显 ...