题目描述
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
在这里,我们只需要考虑其平衡性,不需要考虑其是不是排序二叉树
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def IsBalanced_Solution(self, pRoot):
# write code here
if not pRoot:
return True
left_depth = self.get_depth(pRoot.left)
right_depth = self.get_depth(pRoot.right)
return abs(left_depth-right_depth)<=1 and self.IsBalanced_Solution(pRoot.left) and self.IsBalanced_Solution(pRoot.right)
def get_depth(self, pRoot):
if not pRoot:
return 0
return max(self.get_depth(pRoot.left),self.get_depth(pRoot.right))+1