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

Last Updated: 7 Mar, 2021

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 :

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

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

- 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

Hard

Posted: 17 Apr, 2022

Prime Digit Sum

Hard

Posted: 17 Apr, 2022

Height of Binary Tree

Easy

Posted: 22 Apr, 2022

Height of Binary Tree

Easy

Posted: 22 Apr, 2022

Height of Binary Tree

Easy

Posted: 22 Apr, 2022

Mario And His Princess

Moderate

Posted: 12 May, 2022

Locked Binary Tree

Easy

Posted: 12 May, 2022

8-Queen Problem

Easy

Posted: 19 Dec, 2022