-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbalancedBinaryTree.py
More file actions
49 lines (45 loc) · 1020 Bytes
/
balancedBinaryTree.py
File metadata and controls
49 lines (45 loc) · 1020 Bytes
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
44
45
46
47
48
49
# Definition for a binary tree node
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
# @param root, a tree node
# @return a boolean
def isBalanced(root):
def traverse(node):
if node == None:
return 0
depthLeft = traverse(node.left)
if depthLeft < 0:
return depthLeft
depthRight = traverse(node.right)
if depthRight < 0:
return depthRight
if abs(depthLeft - depthRight) > 1:
return -1
else:
return max(depthLeft, depthRight) + 1
return traverse(root) >= 0
a = TreeNode('a')
b = TreeNode('b')
c = TreeNode('c')
d = TreeNode('d')
e = TreeNode('e')
f = TreeNode('f')
g = TreeNode('g')
a.left = b
a.right = c
b.left = d
b.right = e
c.right = f
e.right = g
assert isBalanced(a) == True
h = TreeNode('h')
i = TreeNode('i')
j = TreeNode('j')
h.left = i
i.left = j
assert isBalanced(h) == False
k = TreeNode('k')
assert isBalanced(k)