Easy!

题目描述:

给定一个二叉树,判断它是否是高度平衡的二叉树。

本题中,一棵高度平衡二叉树定义为:

一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。

示例 1:

给定二叉树 [3,9,20,null,null,15,7]

    3
/ \
9 20
/ \
15 7

返回 true 。

示例 2:

给定二叉树 [1,2,2,3,3,null,null,4,4]

       1
/ \
2 2
/ \
3 3
/ \
4 4

返回 false 。

解题思路:

求二叉树是否平衡,根据题目中的定义,高度平衡二叉树是每一个节点的两个字数的深度差不能超过1,那么我们肯定需要一个求各个点深度的函数,然后对于每个节点的两个子树来进行深度差的比较,时间复杂度为O(NlgN)。

C++解法一:

 class Solution {
public:
bool isBalanced(TreeNode *root) {
if (!root) 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) return ;
return + max(getDepth(root->left), getDepth(root->right));
}
};

上面那个方法正确但不是很高效,因为每一个点都会被上面的点计算深度时访问一次,我们可以进行优化。方法是如果我们发现子树不平衡,则不计算具体的深度,而是直接返回-1。那么优化后的方法为:对于每一个节点,我们通过checkDepth方法递归获得左右子树的深度,如果子树是平衡的,则返回真实的深度,若不平衡,直接返回-1,此方法时间复杂度O(N),空间复杂度O(H)。

C++解法二:

 class Solution {
public:
bool isBalanced(TreeNode *root) {
if (checkDepth(root) == -) return false;
else return true;
}
int checkDepth(TreeNode *root) {
if (!root) return ;
int left = checkDepth(root->left);
if (left == -) return -;
int right = checkDepth(root->right);
if (right == -) return -;
int diff = abs(left - right);
if (diff > ) return -;
else return + max(left, right);
}
};

LeetCode(110):平衡二叉树的更多相关文章

  1. LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15

    110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...

  2. Java实现 LeetCode 110 平衡二叉树

    110. 平衡二叉树 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 1: 给定二叉树 [3,9 ...

  3. LeetCode 110.平衡二叉树(C++)

    给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 1: 给定二叉树 [3,9,20,null,nu ...

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

    问题 给出一棵二叉树,判断它是否在高度上是平衡的. 对于本问题,高度上平衡的二叉树定义为:每个节点的两棵子树的深度差永远不大于1的一棵二叉树. 初始思路 根据定义,思路应该比较直接:递归计算每个节点左 ...

  5. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  6. LeetCode:平衡二叉树【110】

    LeetCode:平衡二叉树[110] 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 ...

  7. Leetcode:110. 平衡二叉树

    Leetcode:110. 平衡二叉树 Leetcode:110. 平衡二叉树 点链接就能看到原题啦~ 关于AVL的判断函数写法,请跳转:平衡二叉树的判断 废话不说直接上代码吧~主要的解析的都在上面的 ...

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

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

  9. LeetCode 110. Balanced Binary Tree (平衡二叉树)

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

  10. 递归 - Leetcode 110 判断二叉树是否为平衡二叉树

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

随机推荐

  1. 利用PyMySQL库连接数据库

    安装与准备 这是python3的库,所以windows下安装不会像python2那样各种奇葩VC错误.是比较方便的傻瓜安装. Windows平台下: py -3 -m pip install PyMy ...

  2. 4-13 object类,继承和派生( super) ,钻石继承方法

    1,object 类 object class A: ''' 这是一个类 ''' pass a = A() print(A.__dict__) # 双下方法 魔术方法 创建一个空对象 调用init方法 ...

  3. Codeforces Round #545 (Div. 2)(B. Circus)

    题目链接:http://codeforces.com/contest/1138/problem/B 题目大意:贼绕口的题目,就是给你两个字符串s1,s2,然后每一个人代表一列,第一列代表技能一每个人是 ...

  4. org.springframework.beans.factory.CannotLoadBeanClassException-估计mapper出参 和 po字段不对应了

    DEBUG [localhost-startStop-1] - Ignoring bean class loading failure for bean 'itemsService'org.sprin ...

  5. java.io.IOException: Server returned HTTP response code: 411 for URL

    今日调用一post方式提交的http接口,此接口在测试环境ip调用时无问题,但在生产环境通过域名调用时一直报如下错误: java.io.IOException: Server returned HTT ...

  6. ORA-03113: end-of-file on communication channel 磁盘慢,数据库启动失败

    磁盘慢,数据库启动失败:解决思路:1.让数据文件offline: 2.删除表空间 SQL> startup pfile='/server/oracle/admin/test/pfile/init ...

  7. Java 进制间的转换

    package com.touch.onlinedu; public class Test { public static void main(String[] args) { // 1 : 0001 ...

  8. java知识点1

    本系列文章源自大神--纯洁的微笑的博客 http://www.cnblogs.com/ityouknow/ 基础篇 JVM JVM内存结构 堆.栈.方法区.直接内存.堆和栈区别 内存结构图 控制参数 ...

  9. string.Empty, "" 和 null 三者的区别

    转载:http://www.cnblogs.com/mxxblog/archive/2013/08/22/3275387.html 这是一个及其常见的问题,网上已经有关于这个问题的很多讨论.但是我觉得 ...

  10. [Docker]CentOS7下Docker安装教程

    想要倒腾Kubernetes的话,第一步就是要会安装Docker,这篇文章讲一讲过程 安装步骤 检查内核版本,必须是3.10以上 uname -r 安装Docker yum -y install do ...