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. linux下查找包含BOM头的文件和清除BOM头命令

    查找包含BOM头的文件,命令如下:   grep -r -I -l $'^\xEF\xBB\xBF' ./   这条命令会查找当前目录及子目录下所有包含BOM头的文件,并把文件名在屏幕上输出.   但 ...

  2. 【转载】利用Unity自带的合图切割功能将合图切割成子图

    虽然目前网上具有切割合图功能的工具不少,但大部分都是自动切割或者根据plist之类的合图文件切割的, 这种切割往往不可自己微调或者很难维调,导致效果不理想. 今天逛贴吧发现了一位网友写的切割合图插件很 ...

  3. Google翻译请求(难点是tk参数)

    业务需求需要将一些文字翻译一下··· 但是直接调用接口收费啊啊啊啊(貌似是前几百万字免费,然后就开始收费了)···· 就想研究一下Google翻译接口... 想模拟Google向服务器发送一个Http ...

  4. 构建spring+mybatis+redis架构时遇到的小异常

    异常一: Caused by: java.lang.NoSuchMethodError: org.springframework.beans.BeanUtils.instantiateClass(Lj ...

  5. VS上利用C#实现一个简单的串口程序记录

    一.背景 工作上需要利用串口往下位机写入数据,VC太老,正好借此机会来熟悉一直很想接触的VS之C#. 感谢Tony托尼哥的串口通信代码,感谢梦真的C#的技术支持. 二.正文 1.项目架构:(以我现有的 ...

  6. 改造 ThinkPHP,弃用 D() 等魔术函数

    ThinkPHP 是国内比较优秀的 PHP 框架,但有些地方不是很好,比如那些 魔术函数 D(),用它返回的类实例,在各个IDE(如 PhpStorm)下根本识别不了,导致如下问题: 1.不支持 代码 ...

  7. 第31天 mvp

    interactor调用接口 Activity包含Presenter,这样界面上的操作就会通知到Presenter. Presenter调用view接口, Activity实现view接口,这样Pre ...

  8. iOS并发编程指南之同步

    1.gcd fmdb使用了gcd,它是通过 建立系列化的G-C-D队列 从多线程同时调用调用方法,GCD也会按它接收的块的顺序来执行. fmdb使用的是dispatch_sync,多线程调用a ser ...

  9. 用CSS画个三角形

    <!DOCTYPE html> <html> <head> <style type="text/css"> #trangle { d ...

  10. Java 7 Concurrency Cookbook 翻译 第一章 线程管理之一

    一.简介 在计算机的世界里,当我们谈论并发时,我们指的是一系列的任务同时运行于一个计算机中.这里说的同时运行,在计算机拥有多于一个处理器或者是一个多核处理器的时候才是真正的同时,在计算机只拥有单核处理 ...