平衡二叉树 牛客网 剑指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)