Binary Search Tree : Lowest Common Ancestor HackerRank Solution


Binary Search Tree : Lowest Common Ancestor HackerRank Solution
Source : https://www.hackerrank.com/challenges/binary-search-tree-lowest-common-ancestor



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;
}

10 comments:

  1. 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

    ReplyDelete
  2. OBCTOP 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/

    ReplyDelete
  3. dj equipments that are built by Sennheiser are the best in my opinion, we always use them when we have a gig, 안전놀이터

    ReplyDelete
  4. We 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

    ReplyDelete
  5. OC 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

    ReplyDelete
  6. World 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

    ReplyDelete
  7. This 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

    ReplyDelete
  8. As 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

    ReplyDelete
  9. The 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é_/

    ReplyDelete
  10. This 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