Leetcode 110 Balanced Binary Tree 二叉树
判断一棵树是否是平衡树,即左右子树的深度相差不超过1.
我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树
/**
* 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:
int depth(TreeNode* root){
if(!root) return ;
else{
return max(depth(root->left), depth(root->right)) + ;
}
}
bool isBalanced(TreeNode* root) {
if(!root) return true;
else if(abs(depth(root->left) - depth(root->right))> ){
return false;
}
else return isBalanced(root->left) && isBalanced(root->right);
}
};
Leetcode 110 Balanced Binary Tree 二叉树的更多相关文章
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- [LeetCode] 110. Balanced Binary Tree 平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode 110. Balanced Binary Tree (平衡二叉树)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- [LeetCode] 110. Balanced Binary Tree 解题思路
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode 110 Balanced Binary Tree(平衡二叉树)(*)
翻译 给定一个二叉树,决定它是否是高度平衡的. (高度是名词不是形容词-- 对于这个问题.一个高度平衡二叉树被定义为: 这棵树的每一个节点的两个子树的深度差不能超过1. 原文 Given a bina ...
- leetcode 110 Balanced Binary Tree(DFS)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode 110. Balanced Binary Tree平衡二叉树 (C++)
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- Leetcode 110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
随机推荐
- Select查询语句2
一.模糊查询 1.语法结构 select*from table_name where column like '%context%' 在使用like运算符时如果不使用通配符“%”,则like的作用与= ...
- Pig Latin
function translate(str) { //return str; var list = ['a','e','i','o','u']; if(list.indexOf(str[0]) &g ...
- java中final,finally和finalize的区别
final,finally和finalize的区别: final:最终的意思,可以修饰类,成员变量,成员方法 修饰类,类不能被继承 修饰变量,变量是常量 修饰方法,方法不能被重写 finally:是异 ...
- memalign vs malloc - 使用O_DIRECT参数open一个文件并读写
听说使用odirect参数打开文件时能够以扇区的单位进行读写. 于是open了一个块设备文件/dev/sdo,当然还要带上读写参数O_RDWR 然后进行读写时出错了. 找了一会发现问题根本在于读写的b ...
- eclipse 快捷键保存在哪里
eclipse 快捷键文件地址:%Workspace%/.metadata/.plugins/org.eclipse.core.runtime /.settings/org.eclipse.ui.wo ...
- html5 上传图片.net实现
jQuery插件之ajaxFileUpload 搞了一夜,还没弄出来随copy了一篇博客... 一.ajaxFileUpload是一个异步上传文件的jQuery插件. 传一个不知道什么版本的上来, ...
- MyBatis框架——mybatis插入数据返回主键(mysql、oracle)
向数据库中插入数据时,大多数情况都会使用自增列或者UUID做为主键.主键的值都是插入之前无法知道的,但很多情况下我们在插入数据后需要使用刚刚插入数据的主键,比如向两张关联表A.B中插入数据(A的主键是 ...
- Android的项目不能直接引用可移植类库的项目解决方法
深圳_exception() 10:25:51 Android的项目不能直接引用可移植类库的项目,但是可以引用可移植类库生成的dll,这就意味着无法直接断电调试可移植类库上海-黄药师() 10:26: ...
- iOS 开发UI篇 -- 懒加载学习
1. 懒加载基本 懒加载--也称为延迟加载,即在需要的时候才加载( 效率低,占用内存小).所谓懒加载,写的是其get方法. 注意:如果是懒加载的话则一定要注意先判断是否已经有了,如果没有那么再去进行实 ...
- FIM相关报错汇总
1.错误1:FIM在修改MA名字的时候报错: The management agent cannot be deleted or renamed because the working directo ...