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. Vscode rg.exe cpu 占用过高

    文件-> 首选项 -> 设置 -> 搜索search.followSymlinks 或者 修改settings.json 添加 "search.followSymlinks ...

  2. 主存储器与CPU的连接

    半导体存储器的读写时间一般在十几至几百毫微秒之间,其芯片集成度高,体积小,片内含有译码器和寄存器等电路.常用的半导体存储器芯片有多字一位片和多字多位片,如16M位容量的芯片可以有16M×1位和4M×4 ...

  3. lua垃圾回收之空表

    故事背景: 自己手动手写的一个lua外部库luaopen_xxx,采用了tolua++1.0.93,编译后得到xxx.dll,当在luajit中require 'xxx'后是正常的,但如果运行环境换成 ...

  4. Monster: half man, half beast and very scary.

    Monster: half man, half beast and very scary. 怪物,半人半兽很吓人.

  5. iStat Menus 的激活密

    Email: @qq.com SN: GAWAE-FCWQ3-P8NYB-C7GF7-NEDRT-Q5DTB-MFZG6-6NEQC-CRMUD-8MZ2K-66SRB-SU8EW-EDLZ9-TGH ...

  6. XtraBackup出现 Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

    解决办法1.建立一个软连接 ln -s /dat/data/mysql/mysql.sock /var/run/mysqld/mysqld.sock 解决办法2:指定--host=127.0.0.1

  7. 将git关联到pycharm

    1. 安装pycharm 2. 安装git 3. 打开pycharm, 点钟File -> settings -> Version Control -> Git 该面板中,填写git ...

  8. 安装IIS步骤图解

    这几日好些网友来找iis安装包,但是因为新浪爱问的共享资料已关闭导致下载链接不可用,笔者在新浪微盘的备份资料只有5.1版,现共享链接如下: IIS5.1 for windows xp下载链接http: ...

  9. vim使用四个空格代替TAB键

    让VIM可以在你写程序的时候自动缩进,并用4个空格代替TAB键. 编辑 ~/.vimrc 保存这四行: “set smartindent”, “set tabstop=4”, “set shiftwi ...

  10. PAT——1051. 复数乘法

    复数可以写成(A + Bi)的常规形式,其中A是实部,B是虚部,i是虚数单位,满足i2 = -1:也可以写成极坐标下的指数形式(R*e(Pi)),其中R是复数模,P是辐角,i是虚数单位,其等价于三角形 ...