-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistanceBetweenNodes.py
84 lines (69 loc) · 2.01 KB
/
distanceBetweenNodes.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
class node(object):
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def pathToNode(root, path, k):
if root is None:
return False
path.append(root.data)
if root.data == k:
return True
if ((root.left != None and pathToNode(root.left, path, k)) or
(root.right != None and pathToNode(root.right, path, k))):
return True
path.pop()
return False
def distance(root, data1, data2):
if root:
path1 = []
pathToNode(root, path1, data1)
path2 = []
pathToNode(root, path2, data2)
i = 0
while i < len(path1) and i < len(path2):
if path1[i] != path2[i]:
break
i = i+1
return (len(path1)+len(path2)-2*i)
else:
return 0
def generatePairs(root, pairs, k):
count = 0
for i in range(len(pairs)):
for j in range(i+1, len(pairs)):
if distance(root, pairs[i], pairs[j]) <= k:
count += 1
return count
def findLeafNodes(root):
if root is None:
return
if root.left is None and root.right is None:
pairs.append(root.data)
return
if root.left:
findLeafNodes(root.left)
if root.right:
findLeafNodes(root.right)
def insertLevelOrder(arr, root, i, n):
if i < n and arr[i] != -1:
temp = node(arr[i])
root = temp
root.left = insertLevelOrder(arr, root.left, 2 * i + 1, n)
root.right = insertLevelOrder(arr, root.right, 2 * i + 2, n)
return root
arr = []
k = int(input())
n = int(input())
if n != -1:
for i in range(n+1):
line = list(map(int, input().split(' ')))
if len([True for i in line if i == -1]) == len(line):
continue
arr = arr+line
n = len(arr)
root = None
root = insertLevelOrder(arr, root, 0, n)
pairs = []
findLeafNodes(root)
print(generatePairs(root, pairs, k))