对称二叉树走不通 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
标签:node,right,cur,leedcode,queue,二叉树,平衡,root,left From: https://www.cnblogs.com/yyyjw/p/18022790