- Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathbalanced-binary-tree.py
More file actions
Latest commit
23 lines (20 loc) · 711 Bytes
/
Copy pathbalanced-binary-tree.py
File metadata and controls
23 lines (20 loc) · 711 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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))
# easy: http://lintcode.com/zh-cn/problem/balanced-binary-tree/