93. Balanced Binary Tree [easy]
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]的更多相关文章
- [leetcode] 110. Balanced Binary Tree (easy)
原题链接 水题 深度搜索每一节点的左右深度,左右深度差大于1就返回false. class Solution { public: bool isBalanced(TreeNode *root) { b ...
- LeetCode_110. Balanced Binary Tree
110. Balanced Binary Tree Easy Given a binary tree, determine if it is height-balanced. For this pro ...
- [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 ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- 平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树
平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树 (a)和(b)都是排序二叉树,但是查找(b)的93节点就需要查找6次,查找(a)的93 ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- 【LeetCode】Balanced Binary Tree 算法优化 解题报告
Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- 110.Balanced Binary Tree Leetcode解题笔记
110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
随机推荐
- Compare DML To Both REDO And UNDO Size
SUMMARY you can remember undo rule the same to redo if you want demo rule that you can look up the ...
- 计算机作业(Excel课程表) 物联网 王罗红
- Spring Boot 的配置文件
Profile 配置 Profile 是 Spring 用来针对不同的环境对不同的配置提供支持的,全局的 Profile 配置使用 application-{profile}.properties ( ...
- 打包工具的核心原理(转自:https://juejin.im/entry/5b223ebd518825748b569bda)
打包工具就是负责把一些分散的小模块,按照一定的规则整合成一个大模块的工具.与此同时,打包工具也会处理好模块之间的依赖关系,最终这个大模块将可以被运行在合适的平台中. 打包工具会从一个入口文件开始,分析 ...
- mysql-存储过程(转载)
本来想自己写存储过程的,但是看到别人写的很全面,就直接转载过来了. 转自(http://www.cnblogs.com/exmyth/p/3303470.html) 14.1.1 创建存储过程 MyS ...
- webpack环境搭建开发环境,JavaScript面向对象的详解,UML类图的使用
PS:因为所有的设计模式都是基于面向对象来完成的,所以在讲解设计模式之前先来过一下面向对象都有哪些知识点 搭建开发环境 初始化npm环境 下载安装nodejs安装即可,nodejs自带npm管理包,然 ...
- 使用loader打包静态文件-样式2
这篇我们了解下css-loader常用的配置项,要配置的话,use里面就不再是一个字符串了 // 打包模块不知道该怎么办,就去模块配置里面该怎么办 module: { // 规则 rules: [{ ...
- bootstrap清除浮动问题
所有的col-样式都是左浮动 <div class="row"> <div class="col-xs-6 col-sm-3">d ...
- selenium通过python字典获取配置
python字典默认的是string item={"browser " : 'webdriver.irefox()', 'url' : 'http://xxx.com'} 如果这样 ...
- Spring@PostConstruct注解和构造方法的调用顺序
先看下@PostConstruct的注解 * The PostConstruct annotation is used on a method that needs to be executed * ...