-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0110. Balanced Binary Tree.py
More file actions
26 lines (25 loc) · 958 Bytes
/
0110. Balanced Binary Tree.py
File metadata and controls
26 lines (25 loc) · 958 Bytes
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
#Runtime: 40 ms, faster than 97.99% of Python3 online submissions for Balanced Binary Tree.
#Memory Usage: 19.3 MB, less than 21.48% of Python3 online submissions for Balanced Binary Tree.
# 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: Optional[TreeNode]) -> bool:
if not root:
return True
def godeep(node):
l_level = r_level = 1
b_l = b_r = True
if node.left:
l_level, b_l = godeep(node.left)
l_level += 1
if node.right:
r_level, b_r = godeep(node.right)
r_level += 1
if abs(l_level - r_level) > 1:
b_l = False
return max(l_level, r_level), b_l and b_r
return godeep(root)[1]