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. Linux 系统网络问题处理集[包含VM处理]

    1.1. 新操作系统ping不同主机: 检查Linux服务器网段是否有etho的IP 查看/关闭防火墙 查看:service iptables status 关闭:service iptables s ...

  2. python 实现插入排序、冒泡排序、归并排序

    def InsertSort(A): '''插入排序算法:传入一个list,对list中的数字进行排序''' print('插入排序前list元素顺序:',A) length=len(A) for i ...

  3. December 24th 2016 Week 52nd Saturday

    The first step is as good as half over. 第一步是最关键的一步. If one goes wrong at the first steps, what shoul ...

  4. memcached源码剖析4:并发模型

    memcached是一个典型的单进程系统.虽然是单进程,但是memcached内部通过多线程实现了master-worker模型,这也是服务端最常见的一种并发模型.实际上,除了master线程和wor ...

  5. navicat连接PostgreSQL报:column “rolcatupdate” does not exist ...错误的解决办法

    avicat premium 连接PostgreSQL出现: column “rolcatupdate” does not exist ... 错误如图: 解决方案: 看看你的navicat 是否为最 ...

  6. javascript对象和函数的几种常见写法

    /** * Created by chet on 15/12/17. */ var En= function (button,func) { //dosth,不能return alert(button ...

  7. Bean Definition从加载、解析、处理、注册到BeanFactory的过程。

    为了弄清楚Bean是怎么来的,花费了大把功夫,现在要把Bean Definition的加载.解析.处理.注册到bean工厂的过程记下来.这只是bean definition 的加载.解析.处理.注册过 ...

  8. Java集合源码 -- Collection框架概述

    1.概述 collection框架是用于处理各种数据结构的,要根据各种数据结构的特点理解它 它能够保存对象,并提供很多的操作去管理对象,当你面临下面的情况时,也许你应该考虑用集合类 1.容器的长度是不 ...

  9. 把dataTable数据转换为Html

    using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Text; ...

  10. seek()和tell()在文件里转移

    Seek()方法允许在输入和输出流移动到任意的位置,seek()有好几种形式.包含:seekp() 方法和seekg()方法,p是put的意思,g是get的意思:其中输入流里用seekg()函数,输出 ...