平衡二叉树

题目描述
输入一棵二叉树,判断该二叉树是否是平衡二叉树。

python solution:

# -*- 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):
        def getheight(root):
            if root is None:
                return 0
            return max(getheight(root.left),getheight(root.right)) + 1
        try:
            if abs(getheight(pRoot.left) - getheight(pRoot.right)) <=1:
                return self.IsBalanced_Solution(pRoot.left) and self.IsBalanced_Solution(pRoot.right)
            else:
                return False
        except:
            return True
原文地址:https://www.cnblogs.com/bernieloveslife/p/10428166.html