#coding:utf-8
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param pRoot TreeNode类
# @return bool布尔型
#
class Solution:
def IsBalanced_Solution(self , pRoot ):
# write code here
#process
##corner
if pRoot == None:
return True
##common
left_max_depth = self.get_max_depth(pRoot.left)
right_max_depth = self.get_max_depth(pRoot.right)
if abs(left_max_depth - right_max_depth) > 1:
return False
is_balanced_left = self.IsBalanced_Solution(pRoot.left)
is_balanced_right = self.IsBalanced_Solution(pRoot.right)
return is_balanced_left and is_balanced_right
def get_max_depth(self, node):
if node == None:
return 0
left_depth = self.get_max_depth(node.left)
right_depth = self.get_max_depth(node.right)
return max(left_depth, right_depth) + 1