leedcode 平衡二叉树

对称二叉树走不通    201 / 228 个通过的测试用例

class Solution:
    def isBalanced(self, root: Optional[TreeNode]) -> bool:
        queue=[root]
        if not root:
            return True
        if not root.left and not root.right:
            return True
        if not root.left or not root.right:
            if  root.right:
                if root.right.right or root.right.left:
                    return False
                if not root.right.right:
                    return True
            if  root.left:
                if root.left.left or root.left.right:
                    return False
                if not root.left.left:
                    return True
        max_depth=0
        while queue:
            level_len=len(queue)
            for i in range(level_len):
                cur_node=queue.pop(0)
                if cur_node.left:
                    queue.append(cur_node.left)
                if cur_node.right:
                    queue.append(cur_node.right)
            max_depth+=1
        queue = [root]
        min_depth = 0
        while queue:
            level_len = len(queue)
            for i in range(level_len):
                cur_node = queue.pop(0)
                if not cur_node.left and not cur_node.right:
                    queue=[]
                    break
                if cur_node.left:
                    queue.append(cur_node.left)
                if cur_node.right:
                    queue.append(cur_node.right)
            min_depth += 1
        print(max_depth,min_depth)
        if max_depth-min_depth<=1:
            return True
        else:
            return False

 改进后的:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isBalanced(self, root: Optional[TreeNode]) -> bool:
        queue=[root]
        if not root:
            return True
        while queue:
            cur_node=queue.pop(0)
            #对左树的处理
            if cur_node.left:#如果指定节点的左节点存在
                left_height=self.cal_depth(cur_node.left)#计算左树的深度
                queue.append(cur_node.left)#并把指定节点的放入队列
            if not cur_node.left:#如果指定节点的左节点不存在  默认左树深度为0
                left_height=0
                
            #对右树的处理
            if cur_node.right:
                right_height=self.cal_depth(cur_node.right)
                queue.append(cur_node.right)
            if not cur_node.right:
                right_height=0
            #如果深度差的绝对值大于1  则返回假并退出
            if abs(left_height-right_height)>1:
                return False
        return True
    def cal_depth(self,root):#计算指定节点的最大深度
        if not root:
            return 0
        queue=[root]
        depth=0
        while queue:
            level_len=len(queue)#计算一层存在节点的个数
            #把下一层存在的节点加入队列
            for i in range(level_len):
                cur_node = queue.pop(0)
                if cur_node.left:
                    queue.append(cur_node.left)
                if cur_node.right:
                    queue.append(cur_node.right)
            depth+=1
        return depth

 

posted @ 2024-02-20 11:49  Junior_bond  阅读(7)  评论(0)    收藏  举报