Minimum Depth of Binary Tree @ LeetCode (Python)
Jump Game II @ LeetCode (Python)

Balanced Binary Tree @ LeetCode (Python)

kitt posted @ 2014年2月18日 17:34 in LeetCode , 2344 阅读
# 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.check(root)[1]
        
    # @param root, a tree node
    # @return a tuple (int height, bool isBalanced)
    def check(self, root):
        if root == None: return (0, True)
        LH, LisB = self.check(root.left)
        RH, RisB = self.check(root.right)
        return ( max(LH, RH) + 1, LisB and RisB and abs(LH - RH) <= 1 )

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter