Last Updated: 20 Dec, 2020

Clone a binary tree with random pointers.

Moderate
Asked in companies
SamsungAmazonExpedia Group

Problem statement

You are given a binary tree. Apart from the left and right child pointers, each node in the given binary tree points to a random node in the given binary tree. You are supposed to return a clone of the binary tree.

Cloning a binary tree means making a deep copy of the input binary tree.

Note :
Two nodes may have the same value associated with them.
The root node will be fixed and will be provided in the function.
Input Format :
The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then the test cases follow.

The first line of each test case contains elements 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. So -1 would not be a part of the tree nodes.

The second line of each test case contains the values of the nodes of the tree in the random order ( -1 for NULL node), connecting the node values of the original tree with random nodes of the tree.

For example, the input for the tree depicted in the below image will be:

alt text

1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1

Explanation :

Level 1 :
The root node of the tree is 1

Level 2 :
Left child of 1 = 2
Right child of 1 = 3

Level 3 :
Left child of 2 = 4
Right child of 2 = null (-1)
Left child of 3 = 5
Right child of 3 = 6

Level 4 :
Left child of 4 = null (-1)
Right child of 4 = 7
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 6 = null (-1)
Right child of 6 = null (-1)

Level 5 :
Left child of 7 = null (-1)
Right child of 7 = null (-1)

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:

1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output Format :
For each test case, print the inorder traversal of the cloned binary tree separated by a single space.
On the next line, the checker will output a ‘1’ if you have cloned the tree successfully; otherwise, it will print ‘0’.

Print the output of each test case in a separate line.
Note :
You don’t need to print anything; It has already been taken care of.
Constraints :
1 <= T <= 100
0 <= N <= 3000
1 <= data <= 10 ^ 5 and data!=-1

Where ‘T’ is the number of test cases, and ‘N’ is the total number of nodes in the binary tree, and “data” is the value of the binary tree node

Time Limit: 1 sec

Approaches

01 Approach

The idea is to use a HashMap to map the tree nodes to each clone node. We will traverse the binary tree and create a clone tree with the random pointer equal to NULL and map each node of the given tree to its clone node. Now we will again iterate through the tree and add the random pointer of each node by looking up the HashMap. 

 

The steps are as follows:

  1. Let’s define a HashMap to map each node to it’s cloned node, let’s say “clonedNodes”.
  2. Recursively traverse the tree, for each node create a new clone node, let’s say our current node is “curr”, and the cloned node is “clone”.
    1. Copy the value, left pointer, and right pointer of the current node to “clone”.
    2. Insert “curr” into the HashMap and map it to “clone”.
    3. Recur for the left and the right subtrees.
  3. Now recursively traverse the given tree and the cloned tree simultaneously, and set the random pointer of the cloned tree using the values from the HashMap.

02 Approach

The idea is to modify the given tree to store the cloned nodes temporarily. We will insert each cloned node between the current node and the left child of the current node without copying the random pointer of the current node. Then we will traverse the tree once again to copy the random pointers to the cloned nodes. And finally, we will traverse the tree once again to restore the original structure of the tree and to separate the cloned tree.

 

The steps are as follows:

  1. Recursively traverse the given tree and for each node create a new cloned node, let’s say our current node is “curr”, and the cloned node is “clone”.
    1. Insert “clone” between “curr” and curr->left.
    2. Recursively insert the cloned nodes in the left and right subtrees.
  2. Now traverse the tree once again and set the random pointer of the cloned nodes to the cloned node corresponding to the curr-> random.
    1. Let’s say the current node is “curr”, and the corresponding cloned node is “clone.”
    2. If curr->random is null then simply change clone->random to null.
    3. Otherwise, change clone->random to curr->random->left.
    4. Recur for the left and the right subtree of “curr”.
  3. Now traverse the tree and remove cloned nodes from the tree and restore the structure of the original tree and the cloned tree.
    1. Let’s say our current node is “curr”, and the corresponding cloned node is “clone”.
    2. If the left pointer of “clone” is null, the simple set curr->left to null.
    3. Otherwise, store the clone->left->left in a variable let’s say “leftptr”, now change the value of curr->left to curr->left->left and change the value of clone->left to “leftptr”.
    4. Recur for the left and the right subtree.
  4. Return the head of the cloned tree.