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.

Solution: 

     bool helperBalanced(int *depth, TreeNode * root){
if(root->left == NULL && root->right == NULL){
(*depth) = ;
return true;
} int left_depth = ;
int right_depth = ;
if(root->left != NULL){
if(helperBalanced(&left_depth, root->left) == false)
return false;
} if(root->right != NULL){
if(helperBalanced(&right_depth, root->right) == false)
return false;
} if(abs(right_depth - left_depth) > )
return false; (*depth) = max(left_depth, right_depth) + ; return true;
} bool isBalanced(TreeNode *root) {
if(root == NULL)
return true;
int depth= ;
return helperBalanced(&depth, root);
}

Balanced Binary Tree [LeetCode]的更多相关文章

  1. 110.Balanced Binary Tree Leetcode解题笔记

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

  2. 110. Balanced Binary Tree - LeetCode

    Question 110. Balanced Binary Tree Solution 题目大意:判断一个二叉树是不是平衡二叉树 思路:定义个boolean来记录每个子节点是否平衡 Java实现: p ...

  3. Balanced Binary Tree——LeetCode

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

  4. Balanced Binary Tree leetcode java

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

  5. Balanced Binary Tree --Leetcode C++

    递归 左子树是否为平衡二叉树 右子树是否为平衡二叉树 左右子树高度差是否大于1,大于1,返回false 否则判断左右子树 最简单的理解方法就是如下的思路: class Solution { publi ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

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

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

  8. LeetCode: Balanced Binary Tree 解题报告

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

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

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

随机推荐

  1. HTML 字符图案

    Dog: <!-- :: :;J7, :, ::;7: ,ivYi, , ;LLLFS: :iv7Yi :7ri;j5PL ,:ivYLvr ,ivrrirrY2X, :;r@Wwz.7r: : ...

  2. symfony安装笔记

    下载http://symfony.com/download,这里版本是2.8 将D:\ApacheServer\php路径添加到环境变量path中,在cmd命令行中可以执行php命令 打开php.in ...

  3. Kafka 0.9+Zookeeper3.4.6集群搭建、配置,新Client API的使用要点,高可用性测试,以及各种坑 (转载)

    Kafka 0.9版本对java client的api做出了较大调整,本文主要总结了Kafka 0.9在集群搭建.高可用性.新API方面的相关过程和细节,以及本人在安装调试过程中踩出的各种坑. 关于K ...

  4. mysql中正则表达式的使用

    mysql中正则表达式的性能要高于like,所以这里总结一下正则表达式的使用. 正则表达式的模式及其含义: 下面举例说明其用法: 建表student: create table student(id ...

  5. CSS的应用

    CSS基础入门 目录 一.   列表.................................................................................. ...

  6. java反射类内容获取

    private void DtoReflect(Object obj, MqDto mqDto) throws Exception { Map map = getMap(mqDto); if(obj= ...

  7. Java SE 基础:标识(zhì)符

    Java SE 基础:标识(zhì)符 一.标识符定义 标识符,就是给类.接口.方法.变量等起名字时的字符序列 二.组成规则与注意事项 1.组成规则 英文大小写字母 数组 $ 和 _ 2.注意事项 不 ...

  8. easyUI-combobox 动态绑定数据源

    前台 <link rel="stylesheet" type="text/css" href="../css/easyui.css"/ ...

  9. windows下的c语言和linux 下的c语言以及C标准库和系统API

    1.引出我们的问题? 标准c库都是一样的!大家想必都在windows下做过文件编程,在linux下也是一样的函数名,参数都一样.当时就有了疑问,因为我们非常清楚 其本质是不可能一样的,源于这是俩个操作 ...

  10. 扩展progress_timer的计时精度

    progress对外输出精度只有小数点后两位(这点可以运行上节程序进行验证),即精确到0.01秒. 我们使用模板技术仿造一个progress_timer编写一个新类:new_progress_time ...