【leetcode】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.
题解:一A的题,写一个递归函数height计算树的高度,然后递归实现isBalance函数即可。代码如下:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isBalanced(TreeNode *root) {
if(root == NULL)
return true;
if(abs(height(root->left)-height(root->right)) > )
return false;
return isBalanced(root->left) && isBalanced(root->right);
}
int height(TreeNode *root){
if(root == NULL)
return ;
int left = height(root->left);
int right = height(root->right);
return +(left>right?left:right);
}
};
【leetcode】Balanced Binary Tree的更多相关文章
- 【LeetCode】Balanced Binary Tree 算法优化 解题报告
Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...
- 【leetcode】Balanced Binary Tree(middle)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【LeetCode】Balanced Binary Tree 解题报告
[题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...
- 【LeetCode】Balanced Binary Tree(平衡二叉树)
这道题是LeetCode里的第110道题. 题目要求: 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
- 【Leetcode】【Easy】Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【LeetCode】145. Binary Tree Postorder Traversal
Difficulty: Hard More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...
- 【LeetCode】124. Binary Tree Maximum Path Sum 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...
随机推荐
- 事务(Transaction)概念和特性
http://baike.baidu.com/view/121511.htm 概念 事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元(unit).事务通常由高级数据库 ...
- CSS遮罩层,全兼容
<script type="text/javascript"> $(function(){ $('#divLocker').css({ "position&q ...
- C++井字棋游戏,DOS界面版
据说有一个能保证不败的算法.明天看看先再写个PVC版的. 正题.今天无聊写了个井字棋游戏,顺便逐渐让自己习惯良好的代码风格,放上来给新手学习学习. jzq2.cpp /* N字棋游戏PVP版,DOS版 ...
- 1045. Favorite Color Stripe (30) -LCS同意元素反复
题目例如以下: Eva is trying to make her own color stripe out of a given one. She would like to keep only h ...
- js 正则表达式 取反
http://www.w3school.com.cn/jsref/jsref_obj_regexp.asp 以匹配中文为例 const test_value = '李钊鸿' if (/[^\u4e00 ...
- Spring事务管理之声明式事务管理:基于TransactionProxyFactoryBean的方式
© 版权声明:本文为博主原创文章,转载请注明出处 案例 - 利用Spring的声明式事务(TransactionProxyFactoryBean)管理模拟转账过程 数据库准备 -- 创建表 CREAT ...
- 用第三方下载工具下载官方XCode独立安装包的方法
用第三方下载工具下载官方XCode独立安装包的方法 下载步骤 下载 aria2 工具配置好并启动 (利用其支持配置Cookie并多线程下载的功能.而迅雷则不支持设置Cookie,所以不支持这种需要登录 ...
- 使用Application Loader上传APP流程解读[APP公布]
本文仅仅是提供一个公布流程的总体思路.假设没有公布经验.建议阅读苹果官方公布文档或者Google搜索具体教程. 1.申请开发人员账号:99美金的(须要信用卡支付),详细流程网上有非常多样例.自行搜索. ...
- 设计模式之前之UML
UML,让系统可视化,让规格和设计文档化的表现方法.下面来简单介绍一下这个UML.
- 多媒体开发之rtp 打包发流---udp 丢包问题
http://blog.csdn.net/acs713/article/details/19339707