Balanced Binary Tree [LeetCode]
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]的更多相关文章
- 110.Balanced Binary Tree Leetcode解题笔记
110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- 110. Balanced Binary Tree - LeetCode
Question 110. Balanced Binary Tree Solution 题目大意:判断一个二叉树是不是平衡二叉树 思路:定义个boolean来记录每个子节点是否平衡 Java实现: p ...
- Balanced Binary Tree——LeetCode
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Balanced Binary Tree leetcode java
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- Balanced Binary Tree --Leetcode C++
递归 左子树是否为平衡二叉树 右子树是否为平衡二叉树 左右子树高度差是否大于1,大于1,返回false 否则判断左右子树 最简单的理解方法就是如下的思路: class Solution { publi ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- LeetCode: Balanced Binary Tree 解题报告
Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a he ...
- LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...
随机推荐
- angularJS——ng-bind指令与插值的区别
在AngularJS中显示模型中的数据有两种方式: 一种是使用花括号插值的方式: <p>{{text}}</p> 另一种是使用基于属性的指令,叫做ng-bind: <p ...
- 004_Intelij 使用,Anonymous Apex
插件安装好之后,就可以正常添加cloud 项目: 注意:免费使用是30天,为了不去买license,在过期后,去修改下机器的日期,这个日期是在安装后的一个月内,改好日期后,启动Intelij 少一个截 ...
- 页面Button/Link 传参数
很多情况下,我们需要在一个标准的页面上添加一个button 或者 是Link, 在点击的过程中想把,一些参数传值到另外一个自定义的页面: 下面这个例子说明是如何操作的 如下图所示,是创建另一个Obje ...
- eclipse安装Veloeclipse
step 1.Help-->install new software-->Add Name:Veloeclipse Value:http://veloeclipse.googlecode. ...
- sqlite嵌入式数据库C语言基本操作(2)
:first-child{margin-top:0!important}img.plugin{box-shadow:0 1px 3px rgba(0,0,0,.1);border-radius:3px ...
- Http组件的介绍
do_Http组件就是实现http/https协议的组件. 首先要说明一下,Do平台只是一个移动端开发的平台,不涉及任何后台服务端的技术,你可以使用你自己任何熟悉的语言和技术来实现服务端,do_Htt ...
- mysql压缩包安装配置
1.在环境变量中添加:MYSQL_HOME:C:\05_study\mysql-5.7.10-winx64,在path路径中加入:%MYSQL_HOME%\bin.配置环境变量不是必须的,只是为了能更 ...
- 5分钟开启Esper之旅
原作者:CoffeeOneSugar 翻译:刘斌华 在我之前发表的文章中,我提到我最近热衷于Complex Event Processing (CEP) (复杂事件处理).简单来说,CEP把数据流作为 ...
- msys2安装
最近在研究编译linux下的软件到windows环境中. 发现了一个比cygwin更好玩的东西,那就是msys2 其实之前也在试玩mingw和mingw64,2016-08-12,当时的最新版本,mi ...
- Android studio Gradle 教程
一 . Gradle基础:https://segmentfault.com/a/1190000002439306 module下的gradle文件: // 声明是Android程序 apply plu ...