【剑指offer python】判断平衡二叉树

题目链接

判断平衡二叉树

题目描述

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

solution

class Solution:
    def TreeDepth(self,pRoot):
        if pRoot == None:
            return True
        return max(self.TreeDepth(pRoot.left) + 1,self.TreeDepth(pRoot.right) + 1)
        
    def IsBalanced_Solution(self, pRoot):
        if pRoot == None:
            return True
        left = self.TreeDepth(pRoot.left)
        right = self.TreeDepth(pRoot.right)
        if abs(left -right)<=1:
            return True

 

你可能感兴趣的:(剑指offer,Python)