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 :
Deletion in the binary tree :
Function DELETE_DEEPEST_NODE( ROOT,‘DEEPEST_NODE’):
Get random node from the binary tree:
Prime Digit Sum
Prime Digit Sum
Height of Binary Tree
Mario And His Princess
Locked Binary Tree
8-Queen Problem