码迷,mamicode.com
首页 > 其他好文 > 详细

树的实现(2)

时间:2020-04-23 12:09:43      阅读:60      评论:0      收藏:0      [点我收藏+]

标签:bin   span   treenode   http   bool   color   png   abs   binary   

技术图片

 

 

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def isBalanced(self, root: TreeNode) -> bool:
        return self.treeHeight(root)>=0
    def treeHeight(self,root):
        if not root:
            return 0
        left = self.treeHeight(root.left)
        right = self.treeHeight(root.right)
        if left>=0 and right>=0 and abs(left-right)<=1:
            return max(left,right)+1
        return -1
        

 

树的实现(2)

标签:bin   span   treenode   http   bool   color   png   abs   binary   

原文地址:https://www.cnblogs.com/topass123/p/12759655.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!