forked from yingl/LintCodeInPython
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbalanced_binary_tree.py
More file actions
Latest commit
21 lines (19 loc) · 653 Bytes
/
Copy pathbalanced_binary_tree.py
File metadata and controls
21 lines (19 loc) · 653 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# -*- coding: utf-8 -*-
classSolution:
"""
@param root: The root of binary tree.
@return: True if this Binary tree is Balanced, or false.
"""
defisBalanced(self, root):
# write your code here
ifnotroot:
returnTrue
else:
ifabs(self._isBalanced(root.left) -self._isBalanced(root.right)) <=1:
returnself.isBalanced(root.left) andself.isBalanced(root.right)
else:
returnFalse
def_isBalanced(self, root):
ifnotroot:
return0
return1+max(self._isBalanced(root.left), self._isBalanced(root.right))