110. Balanced Binary Tree

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.

判断二叉树是否为平衡二叉树。

递归解法:
(1)如果二叉树为空,返回真
(2)如果二叉树不为空,如果左子树和右子树都是AVL树并且左子树和右子树高度相差不大于1,返回真,其他返回假

代码如下:

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isBalanced(TreeNode* root) {
int height;
return isAvl(root, height);
}
bool isAvl(TreeNode* pRoot, int & height)
{
if(pRoot == NULL)
{
height = ;
return true;
}
int heightLeft;
bool resultLeft = isAvl(pRoot->left, heightLeft);
int heightRight;
bool resultRight = isAvl(pRoot->right, heightRight);
if(resultLeft && resultRight && abs(heightLeft - heightRight) <= )
{
height = max(heightLeft, heightRight) + ;
return true;
}
else
{
height = max(heightLeft, heightRight) + ;
return false;
}
}
};

leetcode 110的更多相关文章

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

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

  2. LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15

    110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...

  3. Leetcode 110 Balanced Binary Tree 二叉树

    判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...

  4. LeetCode 110. Balanced Binary Tree (平衡二叉树)

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

  5. 递归 - Leetcode 110 判断二叉树是否为平衡二叉树

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

  6. Leetcode 1-10

    这篇文章介绍Leetcode1到10题的解决思路和相关代码. 1. Two sum 问题描述:给定一个整数数组,返回两个数字的索引,使它们加起来等于一个特定的目标. 例子: Given nums = ...

  7. [LeetCode] 110. Balanced Binary Tree 平衡二叉树

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

  8. Java实现 LeetCode 110 平衡二叉树

    110. 平衡二叉树 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 1: 给定二叉树 [3,9 ...

  9. Leetcode 110. Balanced Binary Tree

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

随机推荐

  1. Linux命令(19)用户权限管理:chown

    linux用户权限: Linux/Unix 是多人多工作业系统,所有的档案皆有拥有者.利用 chown 可以将档案的拥有者加以改变. 一般来说,这个指令只有是由系统管理者(root)所使用,一般使用者 ...

  2. ylbtech-dbs:ylbtech-5,RI(报销发票系统)

    ylbtech-dbs:ylbtech-5,RI(报销发票系统) -- =============================================-- DatabaseName:Pur ...

  3. JAVA 什么时候使用静态

    static所修饰的内容是成员(成员属性.成员方法) 从两方面入手:1.什么时候使用静态的成员属性:当属于同一个类的所有对象出现共享数据时,需要将存储这个共享数据的成员变量用static修饰 2.什么 ...

  4. 怎么安装phpcms?PHPCMS V9安装图文教程

    Phpcms是国内领先的网站内容管理系统, 同时也是一个开源的PHP开发框架.PHPCMS V9目前已提供文章.图片.下载等内容模型,在此基础上可非常方便的扩展出信息.房产.交友.点评等功能.已有的模 ...

  5. Java多线程之join

    1.join方法只有在继承了Thread类的线程中才有. 2.线程必须要start() 后再join才能起作用. 将另外一个线程join到当前线程,则需要等到join进来的线程执行完才会继续执行当前线 ...

  6. javascript delete方法

    学习delete可以参考下面两个博客,写的都很好,本文大部分参考与以下两个博客 http://www.cnblogs.com/windows7/archive/2010/03/28/1698387.h ...

  7. Ext 4.2 RowEditing

    Follow: function () { Ext.define('Follow', { extend: 'Ext.data.Model', idProperty: 'id', fields: [ { ...

  8. 第七届蓝桥杯C++B组省赛

    1.煤球数目 2.生日蜡烛 3.凑算式 4.快速排序 5.抽签 6.方格填数 7.剪邮票 8.四平方和 9.交换瓶子 10.最大比例 今天是周三了,周天刚考完,这次做的还是不好(上次是全省最后一名). ...

  9. Mingyang.net:No identifier specified for entity

    org.hibernate.AnnotationException: No identifier specified for entity: net.mingyang.modules.system.C ...

  10. Regional Changchun Online--Travel(最小生成树&& 并查集)

    Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total S ...