难度简单180
输入一棵二叉树的根节点,判断该树是不是平衡二叉树。如果某二叉树中任意节点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。
示例 1:
给定二叉树 [3,9,20,null,null,15,7]
3 / \ 9 20 / \ 15 7
返回 true
。
示例 2:
给定二叉树 [1,2,2,3,3,null,null,4,4]
1 / \ 2 2 / \ 3 3 / \ 4 4
返回 false
。
# 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:
def balanced(cur):
if not cur:
return 0
left=balanced(cur.left)
if left==-1:
return -1
right=balanced(cur.right)
if right==-1:
return -1
return max(left,right)+1 if abs(left-right)<=1 else -1
return balanced(root)!=-1
二叉树深度
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def maxDepth(self, root: TreeNode) -> int:
if not root:
return 0
return max(self.maxDepth(root.left),self.maxDepth(root.right))+1
# if not root:
# return 0
# q=[root]
# res=[]
# while len(q):
# l=[]
# for i in range(len(q)):
# t=q.pop(0)
# if t.left:
# q.append(t.left)
# if t.right:
# q.append(t.right)
# res.append(l)
# return len(res)