[leetcode]Balanced Binary Tree @ Python
原题地址:http://oj.leetcode.com/problems/balanced-binary-tree/
题意:判断一颗二叉树是否是平衡二叉树。
解题思路:在这道题里,平衡二叉树的定义是二叉树的任意节点的两颗子树之间的高度差小于等于1。这实际上是AVL树的定义。首先要写一个计算二叉树高度的函数,二叉树的高度定义为:树为空时,高度为0。然后递归求解:树的高度 = max(左子树高度,右子树高度)+1(根节点要算上)。高度计算函数实现后,递归求解每个节点的左右子树的高度差,如果有大于1的,则return False。如果高度差小于等于1,则继续递归求解。
代码:
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param root, a tree node
# @return a boolean
def Height(self, root):
if root == None:
return 0
return max( self.Height( root.left ), self.Height( root.right ) ) + 1 def isBalanced(self, root):
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]Balanced Binary Tree @ Python的更多相关文章
- LeetCode: Balanced Binary Tree 解题报告
Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a he ...
- [LeetCode] Balanced Binary Tree 平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode——Balanced Binary Tree(判断是否平衡二叉树)
问题: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- LeetCode - Balanced Binary Tree
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- leetcode -- Balanced Binary Tree TODO
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- [Leetcode] Balanced binary tree平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode Balanced Binary Tree (判断平衡树)
题意:如题,平衡树是指任意一个节点(除了叶子),其左子树的高度与右子树的高度相差不超过1. 思路:递归解决,但是提供的函数不满足递归的要求啊,我们至少得知道高度,又得返回真假,所以另开个函数解决. / ...
- leetcode Invert Binary Tree python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- LeetCode——Balanced Binary Tree
Description: Given a binary tree, determine if it is height-balanced. For this problem, a height-bal ...
随机推荐
- ref:CodeIgniter框架内核设计缺陷可能导致任意代码执行
ref:https://www.seebug.org/vuldb/ssvid-96217 简要描述: 为准备乌云深圳沙龙,准备几个0day做案例. 官方承认这个问题,说明会发布补丁,但不愿承认这是个『 ...
- Laravel框架初学一路由(基本路由)
基本路由 Laravel最基本的路由:接收一个URI和Closure闭包函数 ,提供了定义路由的一种非常简单且富有表达力的方式 Route::get("foo", function ...
- 哪来的gou zi 阿龙(最新更新于1.21日)
众所周知,信息竞赛教室有一个特gou zi的人,叫做阿龙. 这个人呢,特别好玩,特别gou zi 还有一个人,叫Sugar,这个人特别喜欢和阿龙闹,so,一系列爆笑无脑的事就发生了! 1.谁是鱼? 一 ...
- NEUQ OJ 2004:追梦之人 (计数数位dp)
2004: 追梦之人 描述 题目描述: 为了纪念追梦人,粉丝们创造了一种新的数——“追梦数”.追梦数要满足以下两个条件:1.数字中不能出现“7”2.不能被7整除.比如:777和4396就不是追梦数,而 ...
- 【assembly】用汇编写的一个BMP图片读取器
;----------------------------- ;文件满足256色调的 ;----------------------------- Stack Segment ...
- 2018-2019-2 20162318《网络攻防技术》Exp5 MSF基础应用
1.实验内容 1.一个主动攻击实践,如ms08_067 2. 一个针对浏览器的攻击,如ms11_050) 3. 一个针对客户端的攻击,如Adobe 4. 成功应用任何一个辅助模块 2.基础问题回答 2 ...
- py2exe使用方法 (含一些调试技巧,如压缩email 类)(转)
一.简介 py2exe是一个将python脚本转换成windows上的可独立执行的可执行程序(*.exe)的工具,这样,你就可以不用装python而在windows系统上运行这个可执行程序. py2e ...
- VHDL语言实现的任意整数分频器
fpga中,一般外接的晶振是50Mhz,如果电路中一个模块需要25mhz时钟,那么进行一个2分频,这个是相当容易的,下面是一种方法,还有可以用一个二进制计数器实现.这里就不写代码了.easy.同样的原 ...
- HTTP协议中 POST和GET的区别
http://blog.csdn.net/whuslei/article/details/6667095 权威点的说明请参考:http://www.cs.tut.fi/~jkorpela/forms/ ...
- setTimeout(fn, 0) 的作用
在 zepto 源码中,$.fn 对象 有个 ready 函数,其中有这样一句 setTimeout(fn,0); 1 $.fn = { 2 ready: function(callback){ 3 ...