原题地址: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的更多相关文章

  1. LeetCode: Balanced Binary Tree 解题报告

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

  2. [LeetCode] Balanced Binary Tree 平衡二叉树

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

  3. LeetCode——Balanced Binary Tree(判断是否平衡二叉树)

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

  4. LeetCode - Balanced Binary Tree

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

  5. leetcode -- Balanced Binary Tree TODO

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

  6. [Leetcode] Balanced binary tree平衡二叉树

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

  7. LeetCode Balanced Binary Tree (判断平衡树)

    题意:如题,平衡树是指任意一个节点(除了叶子),其左子树的高度与右子树的高度相差不超过1. 思路:递归解决,但是提供的函数不满足递归的要求啊,我们至少得知道高度,又得返回真假,所以另开个函数解决. / ...

  8. leetcode Invert Binary Tree python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  9. LeetCode——Balanced Binary Tree

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

随机推荐

  1. HashMap+双向链表手写LRU缓存算法/页面置换算法

    import java.util.Hashtable; class DLinkedList { String key; //键 int value; //值 DLinkedList pre; //双向 ...

  2. spring boot thymeleaf常用方式

    动态和静态区别 静态页面的return默认是跳转到/static/index.html,当在pom.xml中引入了thymeleaf组件,动态跳转会覆盖默认的静态跳转,默认就会跳转到/template ...

  3. Python 中的面向对象和异常处理

    在之前我们已经说过了 Python 中内置的主要的几种对象类型,(数,字符串,列表,元组和字典).而面向对象的核心人物还没出场呢 .那么我们常说的对象是什么类型的呢,其实他的类型就是“类”.继承封装和 ...

  4. leetcode 链表 两数相加

     两数相加     给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例 ...

  5. CentOS系统下中文文件名乱码

    原文来自:http://www.zhukun.net/archives/7434 CentOS系统下中文文件名乱码 2014/09/01Linux运维centos.Linuxbear 从windows ...

  6. Vue-router浅析(二)

    一.导航守卫 经常在项目中,会对页面做一些权限控制,所以需要对路由变化做出处理,跳转或取消相应的路由. 导航:即路由发生变化.导航守卫即对路由变化做出处理.当然也分几种情况:对全局变化做处理.对&qu ...

  7. Codeforces Round #353 (Div. 2) A. Infinite Sequence 水题

    A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/675/problem/A Description Vasya likes e ...

  8. WindowsPhone&amp;Windows8.1&amp;Windows8&amp;Unity3d 填坑日记

    近期的游戏开发大体上接近尾声,总结了不少关于Unity3d面向Windows几大平台开发时遇到的各种坑以及怎样填坑的经验.总的来说,Windows8.1 Windows8/RT以及WindowsPho ...

  9. The YubiKey NEO

    The YubiKey NEO The YubiKey line of hardware one-time-password (OTP) generators has been on the mark ...

  10. Automate Screen or Button Taps via Tasker : Simulating keypress events

    When using Tasker, sometimes we want to do some automation on screen e.g. screen or button taps. At ...