[LeetCode]题解(python):110 Balanced Binary Tree
题目来源
https://leetcode.com/problems/balanced-binary-tree/
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.
题意分析
Input: a binary tree
Output: True or False
Conditions: 判断一颗树是不是平衡树,平衡树的意思是:对于每个节点而言,左右子树的最大深度不超过1
题目思路
递归判断。
AC代码(Python)
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def Height(self, root):
if root == None:
return 0
return max(self.Height(root.left), self.Height(root.right)) + 1
def isBalanced(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if root == None:
return True
if abs(self.Height(root.left) - self.Height(root.right)) <= 1:
return self.isBalanced(root.left) and self.isBalanced(root.right)
else:
return False
[LeetCode]题解(python):110 Balanced Binary Tree的更多相关文章
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- 110.Balanced Binary Tree Leetcode解题笔记
110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- 110. Balanced Binary Tree - LeetCode
Question 110. Balanced Binary Tree Solution 题目大意:判断一个二叉树是不是平衡二叉树 思路:定义个boolean来记录每个子节点是否平衡 Java实现: p ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 110 Balanced Binary Tree 二叉树
判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- 【leetcode❤python】110. Balanced Binary Tree
#-*- coding: UTF-8 -*-#平衡二叉树# Definition for a binary tree node.# class TreeNode(object):# def _ ...
- [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 ...
随机推荐
- Windows Phone7 快递查询
(1)API去友商100里申请 布局代码: Exp.xaml <phone:PhoneApplicationPage x:Class="WindowsPhone_Express ...
- wp7 HubTile
在最新的Windows Phone Toolkit中我们可以看到HubTile这个控件,首先先了解下什么是HubTile,简单来说,就是允许你给你的应用程序添加些生动或富有意义的瓦片(Tile).Hu ...
- ios cocos2d FPS过低的解决方法
每当运行程序时,左下角的FPS就低到了10,使app很卡, 原来程序主要卡的部分 -(void)draw{ NSDate *startTime = [NSDate date]; [self func] ...
- A+B Problem 详细解答 (转载)
此为详细装13版 转载自:https://vijos.org/discuss/56ff2e7617f3ca063af6a0a3 全文如下,未作修改,仅供围观,不代表个人观点: 你们怎么都在做网络流,不 ...
- Log4j日志级别
日志记录器(Logger)是日志处理的核心组件.log4j具有5种正常级别(Level). 日志记录器(Logger)的可用级别Level (不包括自定义级别 Level), 以下内容就是摘自log4 ...
- linux开启ssh服务
本文概略:1)ubuntu发行版开启ssh.2)centos发行版开启ssh 1.ubuntu发行版安装/开启ssh服务 1.1 安装ssh服务端 sudo apt-get install opens ...
- 外部dtd
引用外部dtd的语法:<!DOCTYPE 根元素 SYSTEM “DTD文档路径”> PUBLIC:公用 SYSTEM:私有 一个xml文件: 引入一个dtd文件(注意文件的后缀是dtd)
- 使用PHP发送邮件的两种方法
使用PHP发送邮件的两种方法 May242013 作者:Jerry Bendy 发布:2013-05-24 22:25 分类:PHP 阅读:2,107 views 抢沙发 今天 ...
- js 创建书签小工具之理论
我们一直在寻找增加浏览体验的方法,有的方法众所周知,有的则鲜为人知.我原本认为书签小工具属于后者,非常令人讨厌的东西.令我非常懊恼的是我发现在这个问题上我完全是错误的.它并不是令人厌烦的,而是以用户为 ...
- ViewData ViewBag TempData
ViewData(一个字典集合类型):传入的key必须是string类型,可以保存任意对象信息,特点:它只会存在这次的HTTP的要求中而已,并不像session可以将数据带到下一个Http要求. ...