leetcode 110
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的更多相关文章
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...
- Leetcode 110 Balanced Binary Tree 二叉树
判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...
- LeetCode 110. Balanced Binary Tree (平衡二叉树)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 递归 - Leetcode 110 判断二叉树是否为平衡二叉树
110. Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- Leetcode 1-10
这篇文章介绍Leetcode1到10题的解决思路和相关代码. 1. Two sum 问题描述:给定一个整数数组,返回两个数字的索引,使它们加起来等于一个特定的目标. 例子: Given nums = ...
- [LeetCode] 110. Balanced Binary Tree 平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Java实现 LeetCode 110 平衡二叉树
110. 平衡二叉树 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 1: 给定二叉树 [3,9 ...
- Leetcode 110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
随机推荐
- mrp功能分类
mrp主要有六大功能子系统:[工程管理子系统].[销售管理子系统].[计划管理子系统].[采购管理子系统].[库存管理子系统].[MRP运行管理子系统].[工程管理子系统]主要功能:产品/物料编码表. ...
- 冲突--ListView与ScrollView冲突的4种解决方案
众所周知ListView与ScrollView都具有滚动能力,对于这样的View控件,当ScrollView与ListView相互嵌套会成为一种问题: 问题一:ScrollView与ListView嵌 ...
- 寒假学习unity的第一天
1.在Assert中创建材质Material,可以为物体附上材质 2.实例化命令Instantiate(要生成的物体,生成的位置,生成物体的选择角度) 3.检测鼠标左键 if(Inhibitor.Ge ...
- linux下安装easy_install的方法
python中的easy_install工具,类似于Php中的pear,或者Ruby中的gem,或者Perl中的cpan,那是相当的爽歪歪了如果想使用 如果想使用easy_install工具,可能需要 ...
- CentOS 6.5 更新163源(转载)
From:http://www.cnblogs.com/buffer/p/3426908.html 众所周知,Centos 有个很方便的软件安装工具 yum,但是默认安装完centos,系统里使用的 ...
- Gatling的进阶二
1. 参数化 Gatling可以很方便使用csv文件进行参数化,例如一个用户信息表: /* user_information.csv */ username,password,accoun ...
- distinct 多个字段
select distinct ID,AA,BB from tName与select distinct ID from tName有什么区别??第一种情况,distinct会不会影响AA,或者BB字段 ...
- js监听浏览器关闭事件
html : <HTML> <HEAD> <title>test</title> </HEAD> <body onbefore ...
- (转)windows下配置nginx+php环境
原文地址 http://www.cnblogs.com/huayangmeng/archive/2011/06/15/2081337.html 刚看到nginx这个词,我很好奇它的读法(engine ...
- arm交叉编译器gnueabi、none-eabi、arm-eabi、gnueabihf、gnueabi区别
命名规则 交叉编译工具链的命名规则为:arch [-vendor] [-os] [-(gnu)eabi] arch – 体系架构,如ARM,MIPSvendor – 工具链提供商os – 目标操作系统 ...