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.

思路:

我居然在这道题上卡了一个小时。关键是对于平衡的定义,我开始理解错了,我以为是要所有叶节点的高度差不大于1.

但题目中的定义是如下这样的:

Below is a representation of the tree input: {1,2,2,3,3,3,3,4,4,4,4,4,4,#,#,5,5}:

        ____1____
/ \
2 2
/ \ / \
3 3 3 3
/\ /\ /\
4 4 4 4 4 4
/\
5 5

Let's start with the root node (1). As you can see, left subtree's depth is 5, while right subtree's depth is 4. Therefore, the condition for a height-balanced binary tree holds for the root node. We continue the same comparison recursively for both left and right subtree, and we conclude that this is indeed a balanced binary tree.

我AC的代码:我觉得我的代码就挺好挺短的。

bool isBalanced3(TreeNode* root){
int depth = ;
return isBalancedDepth(root, depth);
} bool isBalancedDepth(TreeNode* root, int &depth)
{
if(root == NULL) return true;
int depthl = , depthr = ;
bool ans = isBalancedDepth(root->left, depthl) && isBalancedDepth(root->right, depthr) && abs(depthl - depthr) < ;
depth = ((depthl > depthr) ? depthl : depthr) + ;
return ans;
}

其他人的代码,对高度多遍历了一遍,会比较慢:

bool isBalanced(TreeNode *root) {
if (!root) return true;
if (abs(depth(root->left) - depth(root->right)) > ) return false;
return isBalanced(root->left) && isBalanced(root->right);
}
int depth(TreeNode *node){
if (!node) return ;
return max(depth(node->left) + , depth(node->right) + );
}

【leetcode】Balanced Binary Tree(middle)的更多相关文章

  1. 【LeetCode】Balanced Binary Tree 算法优化 解题报告

    Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...

  2. 【leetcode】Number of Islands(middle)

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  3. 【LeetCode】Balanced Binary Tree 解题报告

    [题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...

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

    这道题是LeetCode里的第110道题. 题目要求: 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. ...

  5. 【leetcode】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 (平衡二叉树)

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

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

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

  8. 【leetcode】Combination Sum III(middle)

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  9. 【leetcode】Insertion Sort List (middle)

    Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...

随机推荐

  1. IIS7.5 在已有的WEB网站上配置FTP发布

    IIS7.5 有了很多新特性,例如FashCGI,Rewrite 模块的内置,简易的FTP发布等等,但是即使是微软,也没有详细的文档,本文详细的介绍了如何在现有的WEB网站上建立FTP发布. IIS ...

  2. ie8及其以下浏览器的document.getElementsByClassName兼容性问题

    使用JavaScript访问DOM的一个重大问题是,此过程需要一种通过元素类名称来选择类的类函数,对DOMContentReady,这种类函数的缺失导致开发人员需要自己编写自定义脚本业执行上述任务,许 ...

  3. CocoaPods版本升级

    和往常一样使用CocoaPods管理一个基于FMDB的项目类库 命令行执行 $ pod install [!] The 'master' repo requires CocoaPods 0.32.1 ...

  4. 调用WebService 实现在线双向翻译

    >先了解一下Web Service的基本概念: Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传 ...

  5. 怎样更改wordpress登陆 URL防止恶意注册

    WP 默认的登陆 URL 是 wp-login.php或wp-admin.php,许多spamer会根据这些footprint来收集可注册的wordpress站点,然后你的站内就多出许多垃圾评论.如果 ...

  6. SqlServer中字符串和日期类型的转换

    SQL Server Date 函数 定义和用法 CONVERT() 函数是把日期转换为新数据类型的通用函数. CONVERT() 函数可以用不同的格式显示日期/时间数据. 语法 CONVERT(da ...

  7. Mysql InnoDB行锁实现方式

    Mysql InnoDB行锁实现方式 InnoDB行锁是通过给索引上的索引项加锁来实现的,这一点MySQL与Oracle不同,后者是通过在数据块中对相应数据行加锁来实现的.InnoDB这种行锁实现特点 ...

  8. Nginx的作用

    负载均衡 现在几乎看不到单机奋斗的应用了吧.反向代理服务器可以根据负载均衡算法进行均匀或者自定义的转发.常见的负载均衡算法有:轮转算法(Round Robin).最少连接算法(Least Connec ...

  9. String、StringBuffer、StringBuilder的不同使用场景

    String 字符串常量StringBuffer 字符串变量(线程安全)StringBuilder 字符串变量(非线程安全) 简要的说, String 类型和 StringBuffer 类型的主要性能 ...

  10. GNU KHATA——开源的会计管理软件

    导读 GNU Khata是一个会计工具. 或者,我应该说成是一系列的会计工具集合,它就像经济管理方面的Evernote一样.它的应用是如此之广,以至于它不但可以用于个人的财务管理,也可以用于大型公司的 ...