-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindDuplicateUsingBinaryTree.py
More file actions
52 lines (37 loc) · 1.02 KB
/
FindDuplicateUsingBinaryTree.py
File metadata and controls
52 lines (37 loc) · 1.02 KB
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
50
51
52
import json
class Node:
def __init__(self, val=0):
self.right = ''
self.left = ''
self.value = val
class Tree(Node):
def addNode(self, tree, node, duplicates):
if not tree:
tree = node
elif tree.value > node.value:
if tree.left:
tree.left = self.addNode(tree.left, node, duplicates)
else:
tree.left = node
elif tree.value < node.value:
if tree.right:
tree.right = self.addNode(tree.right, node, duplicates)
else:
tree.right = node
elif tree.value == node.value:
duplicates.append(node.value)
return tree
def buildTree():
arr = [14, 15, 4, 9, 7, 18, 3, 5, 16, 20,4,17, 9, 14, 5]
tree = Tree()
duplicates = []
for v in arr:
node = Node(v)
tree = tree.addNode(tree, node, duplicates)
s = json.dumps(tree.__dict__, default=serialize)
print(s)
print(duplicates)
def serialize(obj):
return obj.__dict__
if __name__ == '__main__':
buildTree()