-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlintcode_93.py
More file actions
29 lines (27 loc) · 789 Bytes
/
lintcode_93.py
File metadata and controls
29 lines (27 loc) · 789 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
27
28
29
# 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:
if root == None:
return True
if self.ishelp(root) == -1:
return False
else:
return True
def ishelp(self, root):
if root == None:
return 0
if root.left == None and root.right == None:
return 1
left = self.ishelp(root.left)
right = self.ishelp(root.right)
if left == -1 or right == -1:
return -1
if left - right > 1 or right - left > 1:
return -1
else:
return max(left, right) + 1