Binary Search Tree : Insertion HackerRank Solution


Binary Search Tree : Insertion HackerRank Solution
Source : https://www.hackerrank.com/challenges/binary-search-tree-insertion



Source : https://www.hackerrank.com/challenges/binary-search-tree-insertion


Solution


// Karthikalapati.blogspot.com
/* Node is defined as :
class Node {
int data;
Node left;
Node right;
}
*/
// Runtime: O(log n) on a balanced tree.
// Space Complexity: O(1)
static Node Insert(Node root, int value) {
/* Create new Node */
Node newNode = new Node();
newNode.data = value;
/* Special case: empty tree */
if (root == null) {
return newNode;
}
/* Iteratively walk tree and insert new Node */
Node curr = root;
while (true) {
if (value <= curr.data) {
if (curr.left == null) {
curr.left = newNode;
return root;
} else {
curr = curr.left;
}
} else {
if (curr.right == null) {
curr.right = newNode;
return root;
} else {
curr = curr.right;
}
}
}
}

10 comments:

  1. He still has the gleam of a villain in his eyes, but he has enough power and gravitas in his performance, especially when we can easily compare him to the original and legendary Spock (credited as “Spock Prime” in the end credits), that it is obvious he was born to fill the shoes. Can Dogs Get Second Hand Smoke

    ReplyDelete
  2. I like this web blog very much, Its a really nice place to read and get info . home appliances in bangalore

    ReplyDelete
  3. Allianz Packers & Movers Pvt. Ltd. are a packing and moving company offers varied packing and moving services such as loading unloading service, ... Packers and Movers Gurgaon to Bhubaneswar

    ReplyDelete
  4. Some states also require designers to pass an exam before they can earn their professional license. With the right education and experience, you can start your own interior design business or pursue a career with an established firm. How to become an Interior Designer

    ReplyDelete
  5. You may also want to consider hiring employees, especially if you plan to do big projects. Next, you will need to create a... https://startupo.fr/question/1399/comment_déclarer_à_la_caf_que_je_suis_en_formation_/

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. To see got with your web site on the other hand location treatment method clearly a very little amount of submits. Flexible technique for likely foreseeable future, I am book-marking at a stretch safeguarded options halt springs up in concert. 우리카지노사이트

    ReplyDelete
  8. Social media marketing is inevitable for brands today, that want to expand their reach and grow their business, whether online or offline. However, for businesses that are just setting foundations, social media promotions can be taxing as well as time-consuming. buy reddit upvotes

    ReplyDelete
  9. The easiest and fastest way is to buy a vfxAlert subscription in the official Google Play store, then XPAY offers two more options for paying by card. vfxAlert PRO

    ReplyDelete
  10. So, that's it. I believe we must all praise it and accept it as our best friend in life, assisting us in achieving our goals, so here we have it. Get Now
    Buy Now
    Click Now
    Know More
    Visit here

    ReplyDelete