美文网首页Leetcode
110. Balanced Binary Tree

110. Balanced Binary Tree

作者: oo上海 | 来源:发表于2016-07-27 07:18 被阅读11次

110. Balanced Binary Tree

题目:
https://leetcode.com/problems/balanced-binary-tree/

难度:
Easy

全程递归中

class Solution(object):
    def isBalanced(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        if root == None:
            return True
        
        lh = self.height(root.left)
        rh = self.height(root.right)
        
        if abs(lh-rh) <= 1 and self.isBalanced(root.left) and self.isBalanced(root.right):
            return True
        return False
        
        
    def height(self, node):
        if node == None:
            return 0
        return 1 + max(self.height(node.left),self.height(node.right))

相关文章

网友评论

    本文标题:110. Balanced Binary Tree

    本文链接:https://www.haomeiwen.com/subject/qgkojttx.html