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.

Example 1:

Given the following tree [3,9,20,null,null,15,7]:

    3
/ \
9 20
/ \
15 7

Return true.

Example 2:

Given the following tree [1,2,2,3,3,null,null,4,4]:

       1
/ \
2 2
/ \
3 3
/ \
4 4

Return false.

Accepted
 
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
def isBalanced(self, root: TreeNode) -> bool:
res = self.helper(root)
return False if res == -1 else True def helper(self, root):
if root is None:
return 0
left = self.helper(root.left)
right = self.helper(root.right)
if left == -1 or right == -1 or abs(left - right) > 1:
return -1
return max(left, right) + 1

[LC] 110. Balanced Binary Tree的更多相关文章

  1. 110.Balanced Binary Tree Leetcode解题笔记

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

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

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

  3. 110. Balanced Binary Tree - LeetCode

    Question 110. Balanced Binary Tree Solution 题目大意:判断一个二叉树是不是平衡二叉树 思路:定义个boolean来记录每个子节点是否平衡 Java实现: p ...

  4. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

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

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

  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

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

  8. Leetcode 110 Balanced Binary Tree 二叉树

    判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...

  9. [LeetCode]题解(python):110 Balanced Binary Tree

    题目来源 https://leetcode.com/problems/balanced-binary-tree/ Given a binary tree, determine if it is hei ...

随机推荐

  1. CTF-域渗透--HTTP服务--命令注入2

    开门见山 1. 启动metasploit 2. 设置参数参数选项 3. 查看最后设置后的结果 4. 启动监听 5. 使用msfvemon制作webshell 6. 开启apache服务 7. 使用ba ...

  2. django-blog:多对多查询

    简单写一下多对多查询model 不是多对多的字段我就没写上来的 class Tag(models.Model): name = models.CharField(max_length=20,verbo ...

  3. pinpoint 单机HBASE数据量过大问题解决

    Pinpoint接入业务监控后数据量大涨,平均每周Hbase数据增量35G左右,数据量太大,需要对数据进行定期清理,否则监控可用性降低. 操作步骤 查找出数据大的hbase表 [root@iZ28ov ...

  4. VUE- 访问服务器端数据 Vue-resource

    VUE- 访问服务器端数据 Vue-resource 1. 安装 vue-resource cnpm install vue-resource --save 安装完毕后,在main.js中导入,如下所 ...

  5. python之路(dingo 框架)

    Python之路  转载自QIMI老师  Python之路 第一篇:Python基础 PyCharm使用秘籍免费视频教程v3 ... 迭代器生成器 模块和常用内置模块 面向对象 面向对象进阶 网络编程 ...

  6. 吴裕雄--天生自然MySQL学习笔记:MySQL 删除数据库

    使用普通用户登陆 MySQL 服务器,可能需要特定的权限来创建或者删除 MySQL 数据库,所以使用 root 用户登录,root 用户拥有最高权限. 在删除数据库过程中,务必要十分谨慎,因为在执行删 ...

  7. 使用Matplotlib

    1.Matplotlib是python的一个绘图库. 2.from matplotlib import pyplot as plt

  8. Jisa's Notebook

    同步IO和异步IO,阻塞IO和非阻塞IO分别是什么,到底有什么区别?不同的人在不同的上下文下给出的答案是不同的.所以先限定一下本文的上下文. 本文讨论的背景是Linux环境下的network IO. ...

  9. 吴裕雄--天生自然 JAVA开发学习:修饰符

    public class InstanceCounter { private static int numInstances = 0; protected static int getCount() ...

  10. ubuntu19.10 相关软件安装

    1.Typora Typora是一款Markdown撰写软件,Linux版本也相当好用. Typora的安装方式如下: # or run: # sudo apt-key adv --keyserver ...