110.平衡二叉树


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

class Solution:
    def isBalanced(self, root: TreeNode) -> bool:
        if root is None:
            return True
        # 分别定义左右子树的高度
        left_depth = 0
        right_depth = 0
        
        if root.left:
            left_depth = self.get_depth(root.left)
        if root.right:
            right_depth = self.get_depth(root.right)
        if abs(left_depth - right_depth) > 1:
            return False
        else:
            return self.isBalanced(root.left) and self.isBalanced(root.right)

    # 获取某一节点对应树的最大高度
    def get_depth(self, root):
        if root is None:
            return 0
        else:
            return max(self.get_depth(root.left), self.get_depth(root.right))+1

优质内容筛选与推荐>>
1、ArcServer +Flex 发布GP服务生成等值面(一) ---建模
2、ios8加入通知栏开始
3、Connection could not be established with host smtp.163.com 阿星小栈
4、Python基础学习
5、小白月赛13 B小A的回文串 (马拉车算法求最长回文子串)


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号