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. Guava 9-I/O

    字节流和字符流 Guava使用术语”流” 来表示可关闭的,并且在底层资源中有位置状态的I/O数据流.术语”字节流”指的是InputStream或OutputStream,”字符流”指的是Reader ...

  2. ajax实现的无刷新分页代码实例

    一.html代码部分: <table class="table style-5">   <thead id="t_head">     ...

  3. Java多线程之线程中断

    该例子说明,Sleep可以被中断,但是I/O和synchronized不能被中断. package Thread.Interrupting; import java.io.IOException; i ...

  4. java中使用session的一些细节

    获取session的时候会产出一个sessionid并且发给客户端,第二次回发的时候再根据该sessionid获取session.如果cookies被禁用,则需要通过URL传入. asp.net下的s ...

  5. sublime 安装常用插件

    1.先要安装Package Control ,ctr+` 打开控制台,复制安装脚本,脚本在https://packagecontrol.io/installation#st3获取. 2.安装插件,ct ...

  6. Microsoft visual studio中文字样输出

    解决办法: 可以尝试下通过: 1.file->高级保存选项-> 2.工具->选项->文本编辑器->自动检测不带签名的UTF-8编码

  7. Ext 项目随笔

    region: This region's layout position (north, south, east, west or center). Read-only. collapsible:t ...

  8. [SQL]当输入表达式得数为一个有效的整数、浮点数、money 或 decimal 类型,那么 ISNUMERIC 返回 1;否则返回 0

    ISNUMERIC ( expression ) 当输入表达式得数为一个有效的整数.浮点数.money 或 :否则返回 .返回值为 确保可以将 expression 转换为上述数字类型中的一种. 注意 ...

  9. (番外)使用DFS和BFS实现拓扑排序

    1.BFS实现 public class Solution { public int[] findOrder(int numCourses, int[][] prerequisites) { int[ ...

  10. 蓝桥杯---地宫取宝(记忆搜索=搜索+dp)

    题目网址:http://lx.lanqiao.org/problem.page?gpid=T120 问题描述 X 国王有一个地宫宝库.是 n x m 个格子的矩阵.每个格子放一件宝贝.每个宝贝贴着价值 ...