-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_balanced.py
More file actions
49 lines (44 loc) · 767 Bytes
/
check_balanced.py
File metadata and controls
49 lines (44 loc) · 767 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
class Node(object):
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def __repr__(self):
return "({})".format(self.data)
def check_balanced(node):
if not node:
return 0
left = check_balanced(node.left)
if left == -1:
return -1
right = check_balanced(node.right)
if right == -1:
return -1
if abs(left - right) > 1:
return -1
return max(left, right) + 1
def main():
root = Node(1)
a = Node(2)
b = Node(3)
c = Node(4)
d = Node(5)
e = Node(6)
f = Node(7)
g = Node(8)
h = Node(9)
i = Node(10)
j = Node(11)
k = Node(12)
root.left = a
root.right = b
b.left = c
c.left = d
c.right = j
b.right = e
a.left = h
a.right = k
h.left = i
print(check_balanced(root))
if __name__ == "__main__":
main()