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
23 lines (20 loc) · 711 Bytes
/
balanced_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
class Solution:
"""
@param root: The root of binary tree.
@return: True if this Binary tree is Balanced, or false.
"""
def isBalanced(self, root):
# write your code here
if not root:
return True
else:
if abs(self._isBalanced(root.left) - self._isBalanced(root.right)) <= 1:
return self.isBalanced(root.left) and self.isBalanced(root.right)
else:
return False
def _isBalanced(self, root):
if not root:
return 0
return 1 + max(self._isBalanced(root.left), self._isBalanced(root.right))
# easy: http://lintcode.com/zh-cn/problem/balanced-binary-tree/