平衡二叉树

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

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
posted @ 2019-03-02 17:51  bernieloveslife  阅读(80)  评论(0编辑  收藏  举报