美文网首页
平衡二叉树

平衡二叉树

作者: GoDeep | 来源:发表于2018-04-04 20:34 被阅读0次

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

    # -*- 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)
    

    相关文章

      网友评论

          本文标题:平衡二叉树

          本文链接:https://www.haomeiwen.com/subject/mjylhftx.html