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. 首层nginx 传递 二级代理,三级代理......多级代理nginx 客户端真实IP的方法

    首层nginx(172.25.10.1):先获取真实IP($remote_addr),再将真实IP传递给X-Forwarded-For    proxy_set_header X-Real-IP $r ...

  2. python面向对象-多继承区别

    #!/usr/local/bin/python3 # -*- coding:utf-8 -*- ''' 构造方法继承策略: 在python2中,经典类是按照深度优先继承构造方法的:新式类是按照广度优先 ...

  3. ecshop 漏洞如何修复 补丁升级与安全修复详情

    目前ecshop漏洞大面积爆发,包括最新版的ecshop 3.0,ecshop 4.0,ecshop2.7.3全系列版本都存在着高危网站漏洞,导致网站被黑,被篡改,被挂马,许多商城系统深受其漏洞的攻击 ...

  4. mybatis 打印SQL

    如果使用的是application.properties文件,加入如下配置: #打印SQL logging.level.com.jn.ssr.supererscuereporting.dao=debu ...

  5. 「LibreOJ#516」DP 一般看规律

    首先对于序列上一点,它对答案的贡献只有与它的前驱和后驱(前提颜色相同)构成的点对, 于是想到用set维护每个颜色,修改操作就是将2个set暴力合并(小的向大的合并),每次插入时更新答案即可 颜色数要离 ...

  6. Java——英文字母---18.10.11

    package lianxi;import java.io.*;import java.util.Scanner;public class file{  public static void main ...

  7. Lucene如何实现多条件搜索?

    有两种方式可以实现, 一是:Lucene搜索API中提供了一个布尔查询器(BooleanQuery),它可以包含多个查询器,每个查询器Occur枚举控制是“and” 还是“or” BooleanQue ...

  8. 一步一步学Linq to sql(二):DataContext与实体

    DataContext DataContext类型(数据上下文)是System.Data.Linq命名空间下的重要类型,用于把查询句法翻译成SQL语句,以及把数据从数据库返回给调用方和把实体的修改写入 ...

  9. Java笔试题-线程编程方面

      Ja 线程编程方面 60.java中有几种方法可以实现一个线程?用什么关键字修饰同步方法?stop()和suspend()方法为何不推荐使用? 答:有两种实现方法,分别是继承Thread类与实现R ...

  10. Anytime项目开发记录1

    关于Android APP 应用设计,我并没有接受过系统的学习. 下面,是按照我一直以来的方法来进行编辑. 由于在程序开始之前并没有画类图,这里简单的讲述一下程序是如何设计的. 自己实现了一个Appl ...