Description

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.

Example

Given binary tree A = {3,9,20,#,#,15,7}, B = {3,#,20,15,7}

A)  3            B)    3
/ \ \
9 20 20
/ \ / \
15 7 15 7

The binary tree A is a height-balanced binary tree, but B is not.

题目不难,判断是不是平衡二叉树(AVL),得保证平衡因子为(1,-1,  0)之一。

思路一:直接按照定义,求出根的左子树、右子树的深度,得出平衡因子,递归求解

public class Solution {
/**
* @param root: The root of binary tree.
* @return: True if this Binary tree is Balanced, or false.
*/
public static int height(TreeNode root){
if(root==null){
return 0;
}else return Math.max(height(root.left),height(root.right))+1;
} public boolean isBalanced(TreeNode root) {
// write your code here
if(root==null)
return true;
else if(Math.abs(height(root.left)-height(root.right))>1)
return false;
//对每个子树进行判断
return isBalanced(root.left)&&isBalanced(root.right);
}

思路二:在求子树的深度时,就判断子树的平衡因子是否满足条件,不满足返回-1,直接结束递归,即不是 height-balanced.。如果没发现哪个子树不满足条件,则返回自己的深度(一定大于等于0)。

public class Solution {
/**
* @param root: The root of binary tree.
* @return: True if this Binary tree is Balanced, or false.
*/
public static int depth(TreeNode root){
//检查下自己
if(root==null)
return 0;
//检查一下左子树
int l=depth(root.left);
if(l==-1)
return -1;
//检查一下右子树
int r=depth(root.right);
if(r==-1)
return -1;
//再检查一下自己
if(Math.abs(l-r)>1)
return -1;
//如果都没问题,就返回自己的真实深度
return 1+Math.max(l,r);
}
public boolean isBalanced(TreeNode root) {
// write your code here
if(depth(root)==-1) return false;
else return true;
}
}

93. Balanced Binary Tree [easy]的更多相关文章

  1. [leetcode] 110. Balanced Binary Tree (easy)

    原题链接 水题 深度搜索每一节点的左右深度,左右深度差大于1就返回false. class Solution { public: bool isBalanced(TreeNode *root) { b ...

  2. LeetCode_110. Balanced Binary Tree

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

  3. [LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II

    Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this p ...

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

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

  5. 平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树

    平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树 (a)和(b)都是排序二叉树,但是查找(b)的93节点就需要查找6次,查找(a)的93 ...

  6. [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)

    Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...

  7. 【LeetCode】Balanced Binary Tree 算法优化 解题报告

    Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...

  8. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  9. 110.Balanced Binary Tree Leetcode解题笔记

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

随机推荐

  1. Apache,php配置

    很多朋友想要在window下配置apache+php+mysql运行环境,其实从这么长时间的经验来看,win2003下还是用iis,如果为了学习或对apache特别熟悉的朋友可以用apache   一 ...

  2. 【解决方案】[XCUITest] WDA is not listening at 'http://localhost:8100/'

    1. 使用Xcode 编译 WebDriver 发现端口为:serverurlhere->http://手机ip:0 <-serverurlhere 2. 解决方案: xcodebuild ...

  3. 一个简单的使用matplotlib作图的例子

    #使用matplotlib作图 import numpy as np import matplotlib.pyplot as plt #x = np.linspace(start, stop, num ...

  4. [CQOI2017]小Q的表格

    题目 神仙题,神仙题 这是一道很适合盯着发呆的题目 看到这个规律 \[ f(a,b)=f(b,a) \] \[ b\times f(a,a+b)=(a+b)\times f(a,b) \] 这也没什么 ...

  5. LayIM.AspNetCore Middleware 开发日记(二)预备知识介绍

    前言 开发一个AspNetCore的中间件需要理解RequestDelegate.另外,还需要理解.NET Core中的依赖注入.还有一个就是内嵌资源的访问.例如:EmbeddedFileProvid ...

  6. Windows下Python3.6安装PIL

    PIL是Python平台事实上的图像处理标准库,需要用到图片的需要导入该模块 一 安装pip https://pip.pypa.io/en/stable/installing/#id8 python ...

  7. php版本跟扩展模块版本不兼容问题

    安装redis扩展后查看时候出现了这样报错: [root@localhost phpredis-develop]# php -m | grep redisPHP Warning: PHP Startu ...

  8. keepalived 的进程/usr/sbin/keepalived -D 只有2个

    操作系统:openSUSE 11.3 (x86_64) /usr/sbin/keepalived -D  只有2条 日志:ls  /var/log/messages* -lrth Can't init ...

  9. 结构体 内存对齐 keil & STM32

    直接 上图 不废话: 第二张图:  把16位 类型的数据 换成 数组 ,这样 达到 节约内存和  方便处理  缓冲区数据

  10. 排列组合:01转换法之lua实现

    c++ 版连接 https://blog.csdn.net/canguanxihu/article/details/46363375 因为项目用的是lua脚本,看了C++版后自己写了一个lua版本的, ...