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, ...
随机推荐
- 【转】深入理解 Session 与 Cookie
Session 与 Cookie 不管是对 Java Web 的初学者还是熟练使用者来说都是一个令人头疼的问题.在初入职场时恐怕很多程序员在面试的时候都被问到过这个问题.其实这个问题回答起来既简单又复 ...
- linux 三大利器 grep sed awk 正则表达式
正则表达式目标 正则表达式单字符: 特定字符 范围字符:单个字符[ ] :代表查找单个字符,括号内为字符范围 数字字符:[0-9],[259] 查找 0~9 和 2.5 .9 中的任意一个字符 小写字 ...
- ajax post data 获取不到数据
ajax post data 获取不到数据,注意 content-type的设置 .post/get关于 jQuery data 传递数据.网上各种获取不到数据,乱码之类的.好吧今天我也遇到了,网 ...
- windows下sqli-labs的搭建及学习(POST篇)
windows下sqli-labs的搭建及学习(GET篇): http://blog.csdn.net/sherlock17/article/details/64454449 Less-11:基于错误 ...
- Dictionary<Tkey.TValue>与SortedList
一.概述 表示Key/Value集合,可以添加删除元素,允许按Key来访问元素.是Hashtable的泛型等效类. 它需要一个相等实现来确定键是否相等,可以使用实现了IEqualityComparer ...
- GCD vs NSOperation
GCD is a lightweight way to represent units of work that are going to be executed concurrently. You ...
- nowcoder模拟赛
R1 D1 普及组... T1/T2 咕 T3 链接:C 小A有一个只包含左右括号的字符串S.但他觉得这个字符串不够美观,因为它不是一个合法的括号串.一个合法的括号串是这样定义的: ()是合法的括号串 ...
- Odoo日历视图
转载请注明原文地址:https://www.cnblogs.com/cnodoo/p/9280604.html 一:日历视图定义 根元素为<calendar>. 主要的属性有: co ...
- jquery全选 反选
//全选 反选 $('#chkAll').on('click',function(){ $('input.chkbox').prop('checked',$(this).prop('checked') ...
- Linux命令总结(转)
1.ls [选项] [目录名 | 列出相关目录下的所有目录和文件 -a 列出包括.a开头的隐藏文件的所有文件 -A 通-a,但不列出"."和".." -l 列出 ...