-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBalancedTree.py
43 lines (38 loc) · 1.18 KB
/
BalancedTree.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'''
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
'''
import sys
sys.path.append("../../datastuctures")
from tree.binarytree import TreeNode
class Solution(object):
def getHeight(self, node):
if not node:
return 0
h = 1
maxHeight = 0
s = [(node, h)]
while s:
n, ch = s.pop()
if n.left:
s.append((n.left, ch + 1))
if n.right:
s.append((n.right, ch + 1))
maxHeight = max(ch, maxHeight)
return maxHeight
def isBalanced(self, root):
if not root:
return True
s = [root]
while s:
n = s.pop()
if abs(self.getHeight(n.left) - self.getHeight(n.right)) > 1:
return False
if n.left:
s.append(n.left)
if n.right:
s.append(n.right)
return True
if __name__ == "__main__":
t1 = TreeNode(1)(TreeNode(2)(TreeNode(3)))
assert False == Solution().isBalanced(t1)