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 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...
随机推荐
- Weblogic AdminServer启动失败,<Security> <BEA-090870> <The realm "myrealm" failed to be loaded
服务器重装,环境配置正常,domain没有变动,启动AdminServer失败. AdminServer_log: <2016-9-29 上午09时43分12秒 GMT+08:00> &l ...
- install phpexcel using composer in thinkPHP
Environment Window 10.1 XAMPP 7.0.9 (PHP 7.0.9) thinkPHP 5.0.1 Steps # visit https://getcomposer.org ...
- wx getlocation
http://www.cnblogs.com/txw1958/p/weixin-web-location.html http://blog.csdn.net/myfmyfmyfmyf/article/ ...
- 关于onethink的迁移站点产生数据库错误
为了支持国产,本人使用了onethink建立了一个自己的站点( 模板世界:www.templatesy.com ),使用至今,虽然碰到了重重困难,还有很多bug,但总算也勉强建了起来. 在近期的一 ...
- jQuery 增加 删除 修改select option .
jQuery获取Select选择的Text和Value: 1. var checkText=jQuery("#select_id").find("option:selec ...
- UNIX网络编程卷1 第一章 简介 读书笔记。
基本没讲什么,一点点计算机网络发展史,一点点socket()简单介绍,最重要的是1.3节协议无关性. 协议无关性: 贯穿整本书的一个重要特性,他主要强调的是 socket是网络协议无关的编程接口. s ...
- Javascript > Eclipse > problems encountered during text search
Reproduce: Ctrl + H, Select "File Search", will encounter eclipse kinds of bug/error alert ...
- 2016年12月30日 星期五 --出埃及记 Exodus 21:25
2016年12月30日 星期五 --出埃及记 Exodus 21:25 burn for burn, wound for wound, bruise for bruise.以烙还烙,以伤还伤,以打还打 ...
- JS控制的几种页面跳转方式和传值
第一种:<script language="javascript" type="text/javascript">window.location.h ...
- JavaACOFramework的各个类介绍(part1 : Ant类)
public abstract class Ant extends Observable implements Runnable { public static int ANT_ID = 1; // ...