平衡二叉树

2018-04-04  本文已影响0人  GoDeep

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

# -*- coding:utf-8 -*-
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None
        
class Solution:
    def TreeDepth(self, r):
        if not r: return 0
        return 1+max(self.TreeDepth(r.left), self.TreeDepth(r.right))
    
    def IsBalanced_Solution(self, r):
        # write code here
        if not r: return True
        return -1<=self.TreeDepth(r.left)-self.TreeDepth(r.right)<=1 and self.IsBalanced_Solution(r.left) and self.IsBalanced_Solution(r.right)
上一篇下一篇

猜你喜欢

热点阅读