-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathanswer.py
26 lines (23 loc) · 882 Bytes
/
answer.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
#!/usr/bin/env python3
#-------------------------------------------------------------------------------
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def isBalanced(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if not root: return True
balance = abs(self.getMaxHeight(root.left)-self.getMaxHeight(root.right))
if balance > 1:
return False
return self.isBalanced(root.left) and self.isBalanced(root.right)
def getMaxHeight(self, root):
if root is None: return 0
return 1 + max(self.getMaxHeight(root.left), self.getMaxHeight(root.right))
#-------------------------------------------------------------------------------