Source : https://www.hackerrank.com/challenges/binary-search-tree-insertion
You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree.You just have to complete the function.
Input Format
You are given a function,
Node * insert (Node * root ,int data) {}
Constraints
- No. of nodes in the tree 500
Output Format
Return the root of the binary search tree after inserting the value into the tree.
Sample Input
4 / \ 2 7 / \ 1 3
The value to be inserted is 6.
Sample Output
4 / \ 2 7 / \ / 1 3 6
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; | |
} | |
} | |
} | |
} |
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
ReplyDeleteI like this web blog very much, Its a really nice place to read and get info . home appliances in bangalore
ReplyDeleteAllianz 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
ReplyDeleteSome 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
ReplyDeleteYou 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_/
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteTo 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. 우리카지노사이트
ReplyDeleteSocial 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
ReplyDeleteThe 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
ReplyDeleteSo, 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
ReplyDeleteBuy Now
Click Now
Know More
Visit here