Insert Into A Binary Search Tree

You have been given a root node of the binary search tree and a positive integer value. You need to perform an insertion operation i.e. inserting a new node with the given value in the given binary search tree such that the resultant tree is also a binary search tree. If there can be more than one possible tree, then you can return any.

Note :

A binary search tree is a binary tree data structure, with the following properties :
    a. The left subtree of any node contains nodes with the value less than the node’s value.
    b. The right subtree of any node contains nodes with the value equal to or greater than the node’s value.
    c. Right, and left subtrees are also binary search trees.
It is guaranteed that,
    a. All nodes in the given tree are distinct positive integers.
    b. The given BST does not contain any node with a given integer value.

Example, below the tree, is a binary search tree.

1

Below the tree is not a BST as node ‘2’ is less than node ‘3’ but ‘2’ is the right child of ‘3’, and node ‘6’ is greater than node ‘5’ but it is in the left subtree of node ‘5’.

1

Input Format :

The first line contains an integer 'T' which denotes the number of test cases or queries to be run.

The first line of each test case contains elements of the tree in the level order form. The line consists of values of nodes separated by a single space. In case a node is null, we take -1 in its place.

The second line of each test case contains a positive integer value ‘val’, denoting the value of the node that is to be inserted in the given BST.

For example, the level order input for the tree depicted in the below image.

1

50  
13 72  
3 25 66 -1  
-1 -1 -1 -1 -1 -1  

Explanation :

Level 1 :
The root node of the tree is 50

Level 2 :
Left child of 50 = 13
Right child of 50 = 72

Level 3 :
Left child of 13 = 3
Right child of 13 = 25
Left child of 72 = 66
Right child of 72 =  ‘Null’


Level 4 :
All children are ‘Null’

The first not-null node(of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.
The input ends when all nodes at the last level are null(-1).

Note :

The above format was just to provide clarity on how the input is formed for a given tree. 

The sequence will be put together in a single line separated by a single space. 

Hence, for the above-depicted tree, the input will be given as:
50 13 72 3 25 66 -1 -1 -1 -1 -1 -1 -1

Output Format :

For each test case, the output will be “1” if you have returned the correct answer, else it will be “0”.

The output of each test case will be printed in a separate line.

Note :

You do not need to input or print anything, and it has already been taken care of. Just implement the given function.

Constraints :

1 <= T <= 5
0 <= N <= 3000
1 <= data <= 10 ^ 9
1 <= val <= 10 ^ 9

Where ‘N’ is the total number of nodes in the given binary tree, ‘data’ is the value of the nodes of the given binary tree, and ‘val’ represents the value of node that is to be inserted in the given tree.

For a single test case, all given ‘data’ and ‘val’ are distinct from each other.

Time Limit: 1sec
CodingNinjas
author
2y
Recursive Approach

The rules of the binary search tree say if any node value is less than the root node value, then this node must lie on the left subtree of the root else will lie on the right subtree...read more

CodingNinjas
author
2y
Iterative Approach

In order to find a correct position for the insertion of a given node, we can find a path from the root node to a node whose left/right child can be the node that is to be inserted. ...read more

Help your peers!
Add answer anonymously...
Cisco Networking Academy Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter