[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 ...
随机推荐
- .NET之类型转换
说起类型转换大家很容易的就会联想到将int类型转换成float类型或者是将double类型转转成int类型之类的转换.当然这可能是大多数人最先接触到的转换方式,也是最简单的转换方式.所谓转换就是从现有 ...
- 虚拟机spark集群搭建
RDD弹性分布式数据集 (Resilient Distributed Dataset) RDD只读可分区,数据集可以缓存在内存中,在多次计算间重复利用. 弹性是指内存不够时可以与磁盘进行交互 join ...
- jQuery事件和动画
1.toggle事件 <!DOCTYPE html> <html> <head lang="en"> <meta charse ...
- Mysql表连接查询
原文地址: https://www.cnblogs.com/qiuqiuqiu/p/6442791.html 1.内联接(典型的联接运算,使用像 = 或 <> 之类的比较运算符).包括相等 ...
- python可变数据和不可变数据
可变数据类型:列表list和字典dict: 不可变数据类型:整型int.浮点型float.字符串型string和元组tuple 可变与不可变是相对“引用地址”来说的.python中的不可变数据类型,不 ...
- BZOJ.3489.A simple rmq problem(主席树 Heap)
题目链接 当时没用markdown写,可能看起来比较难受...可以复制到别的地方看比如DevC++. \(Description\) 给定一个长为n的序列,多次询问[l,r]中最大的只出现一次的数.强 ...
- Trie树理解
前言 Trie树又称单词查找树,字典树,是哈希树的变种: 优点在于:最大限度地减少无谓的字符串比较,查询效率比哈希高: 缺点在于:空间消耗很大: 性质 其基本性质可以归纳为: 跟结点不包括字符,除跟结 ...
- Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 暴力并查集
D. Dividing Kingdom II 题目连接: http://www.codeforces.com/contest/687/problem/D Description Long time a ...
- JS字符串相关操作
01.插入 参数说明:str表示原字符串变量,flg表示要插入的字符串,sn表示要插入的位置 function insert_flg(str,flg,sn){ var newstr="&qu ...
- 小程序获取当前页面路径url
getCurrentPages()[0].route