判断平衡二叉树
作者:互联网
平衡二叉树
输入一棵二叉树的根节点,判断该树是不是平衡二叉树。如果某二叉树中任意节点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。
class Solution:
def isBalanced(self, root: TreeNode) -> bool:
def recur(root):
if not root:
return 0
left =recur(root.left)
if left == -1:
return -1
right = recur(root.right)
if right == -1:
return -1
if abs(left - right)<=1:
return (max(left,right)+1)
else:
return -1
return recur(root) != -1
标签:判断,return,recur,right,二叉树,平衡,root,left 来源: https://blog.csdn.net/wang15735298728/article/details/115581272