判断一棵树是否是平衡树,即左右子树的深度相差不超过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 二叉树的更多相关文章

  1. [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)

    Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...

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

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

  3. [LeetCode] 110. Balanced Binary Tree 平衡二叉树

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

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

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

  5. [LeetCode] 110. Balanced Binary Tree 解题思路

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

  6. LeetCode 110 Balanced Binary Tree(平衡二叉树)(*)

    翻译 给定一个二叉树,决定它是否是高度平衡的. (高度是名词不是形容词-- 对于这个问题.一个高度平衡二叉树被定义为: 这棵树的每一个节点的两个子树的深度差不能超过1. 原文 Given a bina ...

  7. leetcode 110 Balanced Binary Tree(DFS)

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

  8. LeetCode 110. Balanced Binary Tree平衡二叉树 (C++)

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

  9. Leetcode 110. Balanced Binary Tree

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

随机推荐

  1. Jquery 生成时钟

    $(function(){ showTime(); }): function showTime () { var curtime=new Date(); $(".getDateTime&qu ...

  2. 在WINDOWS上安装oracle database 11

    1:在CD-ROM中插入oracle database 11G安装盘会自动运行程序,打开[欢迎使用]窗口 2:弹出[选择安装类型] 3:弹出[制定主目录详细信息]‘oracle基目录’:用于设置环境变 ...

  3. 友盟分享SDK集成步骤

    1.官方注册appID. 2.menifest添加和声明umeng相关的activity以及appKey. 3. // 首先声明一个controller变量,由友盟服务工厂类直接取得友盟社交服务. m ...

  4. 1.Java内存区域

    Java虚拟机在执行java程序的过程中会把他管理的内存划分为若干个不同的数据区域各自用途.创建以及销毁时间各不相同.有的随着虚拟机进行的启动而存在,有的区域依赖于线程的启动和结束而建立以及销毁.如图 ...

  5. abap常用函数

    1.读取生产订单状态函数 call function 'STATUS_READ'           exporting             client           = sy-mandt ...

  6. const in C++

    const关键字是C++中常用的类型修饰符,用法非常灵活,使用const将大大改善程序的健壮性. const的作用 1.  定义const常量: 比如: const int Max = 100; 2. ...

  7. python命令行参数

    〇.python中对应的argc, argv需要模块:sys参数个数:len(sys.argv)脚本名:    sys.argv[0]参数1:     sys.argv[1]参数2:     sys. ...

  8. 网站加载css/js/img等静态文件失败

    网站加载css/js/img等静态文件失败,报网站http服务器内部500错误.而服务器中静态文件存在且权限正常. 从浏览器中直接访问文件,出来乱码.这种问题原因在于iis中该网站mime配置报错,不 ...

  9. LeetCode OJ--Minimum Window Substring ***

    https://oj.leetcode.com/problems/minimum-window-substring/ 模拟题 这道题细节比较多.从左到右扫一遍模拟着做 class Solution { ...

  10. sql行列转换

    首先我们建立一张表,名为scoreInfo,各个字段的设计如下图,分别是name,course,score,表示姓名,成绩与分数,如图所示.