美文网首页
2019-08-22 剑指 平衡二叉树

2019-08-22 剑指 平衡二叉树

作者: mztkenan | 来源:发表于2019-08-22 21:04 被阅读0次

13min,python真的爽,可以返回两个值,一遍通过

class Solution:
    def IsBalanced_Solution(self, pRoot):
        result,_=self.is_balanced(pRoot)
        return result


    def is_balanced(self,pRoot):
        if not pRoot:return True,0
        t1,left_n=self.is_balanced(pRoot.left)
        t2,right_n=self.is_balanced(pRoot.right)
        if t1 and t2 and abs(left_n-right_n)<2:return True,max(left_n,right_n)+1
        return False,0

相关文章

网友评论

      本文标题:2019-08-22 剑指 平衡二叉树

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