-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path110-BalancedBinaryTree.py
51 lines (43 loc) · 1.44 KB
/
110-BalancedBinaryTree.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
47
48
49
50
51
# 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:
# 給定 root 為根節點的二叉樹,判斷是否為高度差大於 1
def isBalanced(self, root: Optional[TreeNode]) -> bool:
self.balanced = True
self.height(root)
return self.balanced
# 給定 root 為根節點的二叉樹,返回最大高度
def height(self, root):
if not root: return 0
if not self.balanced: return 0
left_h = self.height(root.left)
right_h = self.height(root.right)
res = max(left_h, right_h)
if abs(left_h - right_h) > 1:
self.balanced = False
return res + 1
class Solution1:
def maxHeight(self, root) -> int:
"""
Given a root node, return the maximum height of the tree
"""
if root is None: return 0
left_max = self.maxHeight(root.left)
right_max = self.maxHeight(root.right)
if abs(left_max - right_max) > 1:
self.res = False
return max(
left_max,
right_max
) + 1
def isBalanced(self, root: Optional[TreeNode]) -> bool:
"""
Given root node, return if this tree is balanced.
"""
self.res = True
self.maxHeight(root)
return self.res