Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

class Node: def __init__(self, info): self.info = info self.left = None self.right = None self.level = None def __str__(self): return str(self.info) class BinarySearchTree: def __init__(self):

image text in transcribed

class Node: def __init__(self, info): self.info = info self.left = None self.right = None self.level = None

def __str__(self): return str(self.info)

class BinarySearchTree: def __init__(self): self.root = None

def create(self, val): if self.root == None: self.root = Node(val) else: current = self.root while True: if val current.info: if current.right: current = current.right else: current.right = Node(val) break else: break

# Enter your code here. Read input from STDIN. Print output to STDOUT ''' class Node: def __init__(self,info): self.info = info self.left = None self.right = None

// this is a node of the tree , which contains info as data, left , right '''

def lca(root, v1, v2): #Enter your code here

tree = BinarySearchTree() t = int(input())

arr = list(map(int, input().split()))

for i in range(t): tree.create(arr[i])

v = list(map(int, input().split()))

ans = lca(tree.root, v[0], v[1]) print (ans.info)

wesst common ancestorilisy of vi and vil in the hinary scarch tree. In the diaeram abose, the rewst common ancestor of the nodes 4 and 6 is the nove 3 , Node 3 is the lowest rode which has nodes 4 and 6 as descendants. Function Desaription Complete the function ica in the editor belon It show detum a pointer to the lomest common ancestor node ol the lwo values giveli. lis has the fol kaw raparancelers: - 1: a nosec. Input Format The first ie contains an irteger, 13 , the number of ncoes in the zee The sacona line contains is space-saparated integers representing node. dato values. The thire line contains wo space-separated incegers, y1 and 12 . Te use the test data, you N |l have ts ureale the tinaly search tree ycurself. I lece un the platlerm, the L'eee will to crealed lar you. Constraints 121,v:225 1; 2 Output Format Histurn the s pointer to the node that is the lowes common 5 nester of 11 and y2. Sample input \&1 17d2i. Sample Output Jeference to node 4] Explanation LEA of 1 and 7 is 4 , the IDDL in this case. Ruaturn a prinler ua the nade

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Harness The Power Of Big Data The IBM Big Data Platform

Authors: Paul Zikopoulos, David Corrigan James Giles Thomas Deutsch Krishnan Parasuraman Dirk DeRoos Paul Zikopoulos

1st Edition

0071808183, 9780071808187

More Books

Students also viewed these Databases questions