-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathproblem_110.py
27 lines (26 loc) · 918 Bytes
/
problem_110.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
### Problem 110. Balanced Binary Tree (Easy): https://leetcode.com/problems/balanced-binary-tree/
# hint: Perform inorder tarversal and check for each node if depth is greater than 1
class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
ans = True
stack = []
if(root is None):
return True
while(True):
while(root):
stack.append(root)
root = root.left
if(not stack):
break
root = stack.pop()
hl = self.height(root.left)
hr = self.height(root.right)
if(abs(hl - hr) > 1):
return False
root = root.right
return ans
def height(self, root: TreeNode)->int:
if(root is None):
return 0
else:
return max(self.height(root.left), self.height(root.right)) + 1