Problem Link:

http://oj.leetcode.com/problems/balanced-binary-tree/

We use a recursive auxilar function to determine whether a sub-tree is balanced, if the tree is balanced, it also return the depth of the sub-tree.

A tree T is balanced if the following recursive conditions are satisfied:

  1. T's left sub-tree is balanced;
  2. T's right sub-tree is balanced;
  3. The depth of the two sub-trees never differe by more than 1.

The python code is as follows, where a recursive function check the balance in the way of top-down.

# 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 isBalanced(self, root):
return self.balanced_and_depth(root)[0] def balanced_and_depth(self, node):
"""
Return [False, 0] if the sub-tree of node is not balanced,
return [True, depth] otherwise
"""
if not node:
return True, 0
lb, ld = self.balanced_and_depth(node.left)
if not lb:
return False, 0
rb, rd = self.balanced_and_depth(node.right)
if not rb:
return False, 0
if abs(ld-rd) > 1:
return False, 0
return True, 1 + max(ld,rd)

【LeetCode OJ】Balanced Binary Tree的更多相关文章

  1. 【LeetCode OJ】Construct Binary Tree from Preorder and Inorder Traversal

    Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-trave ...

  2. 【LeetCode OJ】Construct Binary Tree from Inorder and Postorder Traversal

    Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-trav ...

  3. 【LeetCode OJ】Flatten Binary Tree to Linked List

    Problem Link: http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ The problem is ask ...

  4. 【LeetCode OJ】Validate Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...

  5. 【LeetCode OJ】Recover Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...

  6. 【leetcode❤python】 257. Binary Tree Paths

    深度优先搜索 # Definition for a binary tree node.# class TreeNode:#     def __init__(self, x):#         se ...

  7. 【leetcode❤python】107. Binary Tree Level Order Traversal II

    #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init ...

  8. 【leetcode❤python】102. Binary Tree Level Order Traversal

    #-*- coding: UTF-8 -*-#广度优先遍历# Definition for a binary tree node.# class TreeNode(object):#     def ...

  9. LeetCode OJ 110. Balanced Binary Tree

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

随机推荐

  1. flex垂直居中

    最近遇到一个令我绞尽脑汁的布局 T.T.T.T,分享下.重点--垂直居中. 布局说明:1. 场次为一场比赛 2. 比赛双方是交战的两个队伍 3. 一场比赛可以有多种玩法,所以场的每个玩法的布局的高度都 ...

  2. 为ssh增加选项

    在使用ssh的时候,可以看到ssh有很多功能,什么-o , -e等等.如下图 需求,想要给ssh增加一个参数的功能.比如说我现在的需求就是执行ssh的时候可以增加一个选项,给我每次ssh的操作搭一个标 ...

  3. My97日期控件 选择日期区间

    <script language="javascript" type="text/javascript" src="My97DatePicker ...

  4. hdu 2177 取(2堆)石子游戏(威佐夫博奕)

    题目链接:hdu 2177 这题不是普通的 Nim 博弈,我想它应该是另一种博弈吧,于是便推 sg 函数打了个 20*20 的表来看,为了方便看一些,我用颜色作了标记,打表代码如下: #include ...

  5. 【教程】Asset Server(联合开发)

    Unity Asset Server下载 https://unity3d.com/cn/unity/team-license http://tieba.baidu.com/p/2419391804 W ...

  6. Echarts的基本用法

    首先需要到导入echatrs.js文件 <script src="dist/echarts.js"></script> 路径配置 require.confi ...

  7. 【python】selenium+python自动化测试环境搭建

    参考资料: http://www.cnblogs.com/fnng/archive/2013/05/29/3106515.html http://www.easonhan.info/python/20 ...

  8. WebView注入Java对象注意事项

    在android4.2以前,注入步骤如下: webview.getSetting().setJavaScriptEnable(true); class JsObject { public String ...

  9. uva 211(dfs)

    211 - The Domino Effect Time limit: 3.000 seconds A standard set of Double Six dominoes contains 28 ...

  10. sqlserver中创建包含事务的存储过程

    什么是事务     事务时包含1条或多条语句的逻辑单元.事务中的语句是一个整体,要么一起提交,要么一起撤销.事务在提交前可以回滚,一旦提交就不能撤销修改了,是永久性的修改.   为什么使用事务     ...