-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnodesdistance.py
More file actions
58 lines (51 loc) · 1.43 KB
/
nodesdistance.py
File metadata and controls
58 lines (51 loc) · 1.43 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
53
54
55
56
57
# Python Program to find distance between
# n1 and n2 using one traversal
class Node:
def __init__(self, data):
self.data = data
self.right = None
self.left = None
def findancesstor(root,val1,val2):
if root:
if root.data==val1 or root.data==val2:
return True
leftbool = findancesstor(root.left, val1, val2)
rightbool = findancesstor(root.right, val1, val2)
if leftbool and rightbool :
return root
if leftbool and not rightbool :
return root.left
if not leftbool and rightbool :
return root.right
if not leftbool and not rightbool:
return False
else:return
def computedistance(root,n1,n2,counter):
if root:
if root.data == n1 or root.data==n2:
return counter
leftval=computedistance(root.left,n1,n2,counter+1)
rightval=computedistance(root.right, n1, n2, counter+1)
if not leftval and rightval:
return rightval
if leftval and rightval:
return leftval+rightval
if leftval and not rightval:
return leftval
else:
return
# Driver Code to test above functions
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.right.right= Node(7)
root.right.left = Node(6)
root.left.right = Node(5)
#root.right.left.right = Node(8)
n1=4
n2=3
common_node=findancesstor(root,n1,n2)
distance=computedistance(common_node,n1,n2,0)
print("the distance betweeen the two node is ",distance)
#dist = distance(root, 2, 5)