New update is available. Click here to update.

Last Updated: 7 Mar, 2021

Difficulty: Moderate

- ‘I’ ‘VAL’: In this query, Ninja has to insert a Node with the value ‘VAL’ in the binary tree.
- ‘D’ ‘VAL’: In this query, Ninja has to delete a Node with the value ‘VAL’ from the binary tree.
- ‘R’: In this query, Ninja has to print a random node from the tree.

```
The first line of input contains an integer 'T' representing the number of test cases. Then the test cases follow.
The next line of each test case contains an integer ‘Q’ representing the number of queries to Ninja.
The next line, ‘Q’ lines of each test case, contain a character and integer representing which type of query is to perform on the binary tree and the value of the node.
```

```
For each test case, return the value of the Random Node obtained from the get Random Node query.
Print the output of each test case in a separate line.
```

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

```
1 <= ‘T’ <= 10
1 <= ‘Q’ <= 10000
Char = {‘I’, ‘D’, ‘R’}
1 <= ‘VAL’ <= 100000
Time Limit: 1 second
```

In this problem, we have to implement a binary tree class from scratch. We have to deal with 3 queries: Insert a node in the binary tree, Delete a node in the binary tree and get a random node in the binary tree. So we make three functions to deal with these three queries.

For inserting a node in the binary tree, first, we do iterative level order traversal in the given binary tree using a queue. If we find a node whose left/right child is empty, we make a new node with ‘VAL’ as the data of the node and add this node into the left/right child in the binary tree.

For deleting a node in the binary tree, first, we start from the root of the binary tree then find the deepest and rightmost node of the binary tree and the node which we want to delete. Then we replace the deepest rightmost node value with the node to be deleted. Then we delete the deepest right node.

For finding a random node in the binary tree, first, we store the inorder traversal of the binary tree in an array/list. Then we generate a random number between 0 to a number of nodes - 1 and return the value which is present at that random index in the array/list.

Here is the algorithm :

**Insertion in the binary tree :**

- If the root of the binary tree is null:
- Return a new node with the value
*‘VAL’*.

- Return a new node with the value
- We declare a queue in which we store nodes of the binary tree.
- Add the root of the binary tree in the queue.
- We run a loop while the queue is not empty:
- Remove the first element from the queue.
- If the left node of the current node is
*‘NULL’*:- Add a new node with the value
*‘VAL’*to the left of the current node. - Return root.

- Add a new node with the value
- If the right node of this node is
*‘NULL’*:- Add a new node with the value
*‘VAL’*to the right of the current node. - Return root.

- Add a new node with the value
- Add left and right nodes of the current node into the queue.

**Deletion in the binary tree :**

- If the root node is
*‘NULL’:**Return.*

- If we are at the leaf node of the binary tree:
- Return.

- We declare a queue in which we store nodes of the binary tree.
- Add the root of the binary tree in the queue.
- We declare a node
*‘NODE_TO_BE_DELETED’*and*‘DEEPEST_NODE’*in which we store a node that we have to delete and the rightmost deepest node in the binary tree. - We run a loop while the queue is not empty:
- Remove the first element from the queue and store into
*‘DEEPEST_NODE’* - If the
*‘DEEPEST_NODE’*value is equal to*‘VAL’:**‘NODE_TO_BE_DELETED’ = ‘DEEPEST_NODE’*

- If the left node of the
*‘DEEPEST_NODE’*is not*‘NULL’:*- Add the left node of the
*‘DEEPEST_NODE’*into the queue.

- Add the left node of the
- If the right node of the
*‘DEEPEST_NODE’*is not*‘NULL’:*- Add right node of the
*‘DEEPEST_NODE’*into the queue

- Add right node of the

- Remove the first element from the queue and store into
- If
*‘NODE_TO_BE_DELETED’*is not*‘NULL’*:*‘DEEPEST_NODE_VAL’ = ‘DEEPEST_NODE.DATA’***DELETE_DEEPEST_NODE( ROOT,‘**DEEPEST_NODE**’).***‘NODE_TO_BE_DELETED.DATA’ = ‘DEEPEST_NODE_VAL’.*

Function** ****DELETE_DEEPEST_NODE****( ****ROOT****,‘***DEEPEST_NODE***’):**

- We traverse on the binary tree using level order traversal.
- If the node of the binary tree is
*‘DEEPEST_NODE’:*- Remove this node.

**Get random node from the binary tree:**

- We declare an array/list
*‘ALL_NODES’*in which we store all nodes of the binary tree. - We traverse on the binary tree and store all the nodes in
*‘ALL_NODES’.* - We generate a random number between 0 to a number of nodes - 1.
- Return the value which is present at that random index.

SIMILAR PROBLEMS

Prime Digit Sum

Posted: 17 Apr, 2022

Difficulty: Hard

Prime Digit Sum

Posted: 17 Apr, 2022

Difficulty: Hard

Height of Binary Tree

Posted: 22 Apr, 2022

Difficulty: Easy

Mario And His Princess

Posted: 12 May, 2022

Difficulty: Moderate

Locked Binary Tree

Posted: 12 May, 2022

Difficulty: Easy

8-Queen Problem

Posted: 19 Dec, 2022

Difficulty: Easy

Popular Interview Problems: