Given a binary search tree and data of two nodes, find 'LCA' (Lowest Common Ancestor) of the given two nodes in the BST.
LCA
LCA of two nodes A and B is the lowest or deepest node which has both A and B as its descendants.
Example:
In this example, the green coloured node is the LCA to A and B.
Alt Text
Note:
It is defined that each node is a descendant to itself, so, if there are two nodes X and Y and X has a direct connection from Y, then Y is the lowest common ancestor.
Example:
Alt Text
Note:
1. If out of 2 nodes only one node is present, return that node.
2. If both are not present, return -1.
3. all the node data will be unique.
Input format:
The first line of input contains data of the nodes of the tree in level order form. The data of the nodes of the tree is separated by space. If any node does not have left or right child, take -1 in its place. Since -1 is used as an indication whether the left or right nodes exist, therefore, it will not be a part of the data of any node.
The following line of input contains two integers, denoting the value of data of two nodes of given BST.
Output Format:
The first and only line of output contains the data associated with the lowest common ancestor node.
Constraints:
Time Limit: 1 second
Sample Input 1:
8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1
2 10
Sample Output 1:
8
Sample Input 2:
8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1
2 6
Sample Output 2:
5
Sample Input 3:
8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1
12 78
Sample Output 3:
-1
*/.
Step by stepSolved in 3 steps with 1 images
- How to announce a Linked List Node with three characters in C language, same as the example shown in teaching materials For example is the following figure. EAT O a. struct Node { char *EAT[3]; struct Node next; } Node; O b. struct { Char Three; Link *Next; }; O c. typedef Linked Node { Character EAT; Arrow Link; O d. struct Node { char Three; Arrow Link; }; O e. typedef structure Node { char Name[3]; structure Node *Next; }; O f. structure List Node { Three Character; Link; } O g. typedef struct Node { char name[3]; struct Node *next; } node_C; Oh. typedef struct Node { char EAT; struct *Next; };arrow_forwardPlease fill in the code gaps if possible. This problem has been giving me trouble. Any help is appreciated. public class Node { private String element; // we assume elements are character strings private Node next; /* Creates a node with the given element and next node. */ public Node(String s, Node n) { //fill in body } /* Returns the element of this node. */ public String getElement() { //fill in body } /* Returns the next node of this node. */ public Node getNext() { //fill in body } // Modifier methods: /* Sets the element of this node. */ public void setElement(String newElem) { //fill in body } /* Sets the next node of this node. */ public void setNext(Node newNext) { //fill in body } }arrow_forwardPlease help with C++ question in image. Thank you.arrow_forward
- Please use the alphabet shown in the screenshot below to code a Depth-first search (DFS) algorithm in C++.arrow_forward2. You have a family tree diagram like the following, very smallI, sample tree and file. It is stored as a file with the following format (parent: , one line for all people in the tree). Your task is to be able to find the descendants of any person in a family tree, e.g. the descendants of p1 are p4.p5,p7,p8 in O(|P|) time, where P is the set of people in the graph. "Sketching" the approach is fine but you may give full pseudocode, just reuse an algorithm from class, perform some obvious conversion (Adjacency Matrix to/from Array List, etc.) or some combination of these. You may not use depth first search or breadth first search. You may preprocess the input with any polynomial approach and may assume the |P| people in the tree are labelled po, p1, .p. p0: p4, p5 p1: p4, p5 p2: p6 p3: p6 p4: p4 p5: p7, p8 p6: p7, p8 p7: p8: p7 p8 Preprocess File By: How to retrieve descendants (O(|P|) time, max): getDescendants(Person ):arrow_forwardCreate a function that uses Node * pointing to root of AVL Tree as an input and returns the height of the AVL tree in O(log(n)).arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education