平衡二叉树 牛客网 剑指Offer

版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/DarrenXf/article/details/82497178

平衡二叉树 牛客网 剑指Offer

  • 题目描述
  • 输入一棵二叉树,判断该二叉树是否是平衡二叉树。
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    def TreeMaxDepth(self, pRoot):
        if pRoot == None:
            return 0
        return max(self.TreeMaxDepth(pRoot.left),self.TreeMaxDepth(pRoot.right)) + 1

    def IsBalanced_Solution(self, pRoot):
        if pRoot == None:
            return True
        if abs(self.TreeMaxDepth(pRoot.right) - self.TreeMaxDepth(pRoot.left)) > 1:
            return False
        return self.IsBalanced_Solution(pRoot.left) and self.IsBalanced_Solution(pRoot.right)

猜你喜欢

转载自blog.csdn.net/DarrenXf/article/details/82497178