Source : https://www.hackerrank.com/challenges/binary-search-tree-lowest-common-ancestor
You are given pointer to the root of the binary search tree and two values and . You need to return the lowest common ancestor (LCA) of and in the binary search tree.
In the diagram above, the lowest common ancestor of the nodes and is the node . Node is the lowest node which has nodes and as descendants.
Function Description
Complete the function lca in the editor below. It should return a pointer to the lowest common ancestor node of the two values given.
lca has the following parameters:
- root: a pointer to the root node of a binary search tree
- v1: a node.data value
- v2: a node.data value
Input Format
The first line contains an integer, , the number of nodes in the tree.
The second line contains space-separated integers representing values.
The third line contains two space-separated integers, and .
To use the test data, you will have to create the binary search tree yourself. Here on the platform, the tree will be created for you.
Constraints
The tree will contain nodes with data equal to and .
Output Format
Return the a pointer to the node that is the lowest common ancestor of and .
Sample Input
64 2 3 1 7 61 7
and .
Sample Output
[reference to node 4]
Explanation
LCA of and is , the root in this case.
Return a pointer to the node.
Source : https://www.hackerrank.com/challenges/binary-search-tree-lowest-common-ancestor
Solution
// Karthikalapati.blogspot.com | |
/* Node is defined as : | |
class Node { | |
int data; | |
Node left; | |
Node right; | |
} | |
*/ | |
// Assumes tree has unique values. | |
// This problem is simpler since we're given a binary SEARCH tree. | |
// Time Complexity: O(log n) on a balanced tree | |
// Space Complexity: O(1) | |
static Node lca(Node n, int v1, int v2) { | |
while (n != null) { | |
if (n.data > v1 && n.data > v2) { | |
n = n.left; | |
} else if (n.data < v1 && n.data < v2) { | |
n = n.right; | |
} else { | |
break; | |
} | |
} | |
return n; | |
} |
5 Star Roof Care have been providing Surrey & London quality and competitively priced roofing services for over 30 years. We offer a range of roofs for installation, from lead roofs to Flat, pitched and GRP roofs. Our roofers are fully qualified and trained to give the best possible customer service. Roofers in London
ReplyDeleteOBCTOP adalah situs judi slot online resmi terpercaya yang juga merupakan salah satu agen slot resmi di Indonesia yang menyediakan permainan slot online yang ... https://saratogapartnership.org/
ReplyDeletedj equipments that are built by Sennheiser are the best in my opinion, we always use them when we have a gig, 안전놀이터
ReplyDeleteWe offer a complete solution of Packing and moving services Greater Noida to Kota as per our customer requirement at affordable prices with 100% satisfaction. Packers and Movers Gurgaon to Chandigarh
ReplyDeleteOC BREEZE VIP SERVICE Welcome to OC BREEZE VIP SERVICE, where the comfort, safety, and satisfaction of our customers are the goals we strive for ground transportation services
ReplyDeleteWorld class treatment at Andersen Family Care Here at Andersen Family Care, you are never just a patient–you are so much more! Book An Appointment Andersen ... Acute Care service
ReplyDeleteThis North East Jobs board gives you access to thousands of current jobs across the North East and UK covering an extensive range of sectors and locations, that ... quick job search
ReplyDeleteAs a full-service lawn maintenance company servicing the Charlotte & surrounding areas, our core intention is to create environments that meet our clients’ needs and exceed their expectations. We strive to provide the highest quality of service and workmanship in a timely manner, while enhancing the beauty and value of every client’s property. Leaf Removal Services
ReplyDeleteThe contract must specify the powers of the legal guardian, the duration of their mandate and the compensation they will receive for their time and efforts. https://startupo.fr/question/1424/comment_monter_sa_société_/
ReplyDeleteThis is Bet 114 that informs you of everything about the Toto community. We deliver the fastest and most accurate news, including various information such as Toto sites, private Toto, and major playgrounds, features of eat-and-run sites, and eat-and-run verification. 토토커뮤니티
ReplyDelete