Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Last Updated: 24 Mar, 2021

Easy

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

```
The first line 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 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 is depicted in the below image.
```

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

```
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).
```

```
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
```

```
The output will be “1” if you have returned the correct answer, else it will be “0”.
```

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

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. We can think of a recursive solution by breaking the task into a subtask, as any node can be either on the left subtree or right subtree of the root node. In BST we can find which subtree (left/right) to follow next by comparing the given value and root node’s value.

For example, if the given value is ‘3’ and the root node’s value is ‘5’ the node will be inserted in the left subtree of the root node as ‘3 < 5’. Similarly, if the given value is greater than the root node’s value we can assure that the node will insert on the right subtree of the root node. For a given tree we can find a required position by recursively calling the function to find the position in left subtree or right subtree after comparing values of nodes. When we reach a subtree whose root node is ‘Null’, then this means that we have reached the required position.

- Base case - If the root is NULL, return a tree node with a value same as the given value.
- If the given value is greater than the value of the root node, then recursively call the function for the right subtree and update the right subtree with the returned tree from the recursive function.
- Else, call the function for the left subtree and update the left subtree with the returned tree from the recursive function.
- Return root node.

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. We can follow from root to down the tree by choosing the left subtree or the right subtree by comparing the value of the root node of the subtree with the given value.

- If the root is NULL
- Return new node with value same as the given value.

- Declare a variable node say ‘temp’ and initialize it with the root node.
- Run a loop until ‘temp’ is not NULL.
- If the given value is greater than the ‘temp -> val’
- Set temp = temp -> right i.e. ( move to right subtree ).

- Else
- Set temp = temp -> left i.e. ( move to left subtree ).

- If the given value is greater than the ‘temp -> val’
- We have reached the required position, So set temp to the new node with the given value.
- Return the ‘root node’.