-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem110.py
46 lines (38 loc) · 1.29 KB
/
problem110.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# <---coding: utf-8--->
# @Time: 2021/12/25/23:42
# @Author = posiondy
# @Email: 1547590574@qq.com
# @Software: PyCharm
# 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: TreeNode) -> bool:
def height(root: TreeNode) -> int:
if not root:
return 0
leftHeight = height(root.left)
rightHeight = height(root.right)
if leftHeight == -1 or rightHeight == -1 or abs(leftHeight - rightHeight) > 1:
return -1
else:
return max(leftHeight, rightHeight) + 1
return height(root) >= 0
class Solution:
def isBalanced(self, root: TreeNode) -> bool:
def MaxHeight(root: TreeNode) -> int:
if not root:
return 0
else:
return max(MaxHeight(root.left), MaxHeight(root.right)) + 1
def MinHeight(root: TreeNode) -> int:
if not root:
return 0
else:
return min(MinHeight(root.left), MinHeight(root.right)) + 1
if MaxHeight(root) - MinHeight(root) > 1:
return False
return True