Timer is Running

Associated Course(s):
Amazon SDE Test Series

Given a Binary Tree having random pointers clone the Binary Tree. The task is to complete the function** cloneTree** which take one argument the root of the tree to be copied and should return the root of the cloned tree.

**Input:**

The first line of input contains the no of test cases. Then T test cases follow. Each test case contains 2 lines the first line contains and integer N denoting the no of edges of the tree and then in the next line are N space separated queries .The query on tree are of three types

a) a b L (Represents that b is the left child of a)

b) a b R (Represents that b is the right child of a)

c) a b X (Represents a random pointer from node a to node b)

**Output:**

Your function should return the root of the cloned tree. The output will be 1 if the tree is successfully cloned.

**Your Task:**

No need to read any input/output. Just complete the function **cloneTree() **which takes root of the tree as a parameter and returns the root of the cloned tree.

**Expected Time Complexity: **O(N).

**Expected Auxiliary Space: **O(N).

**Constraints:**

1 <=T<= 30

1 <=Number of nodes<= 100

1 <=Data of a node<= 1000

**Example(To be used only for expected output):
Input**

1

6

6 3 L 6 8 R 3 1 L 3 5 R 1 3 X 5 6 X

**Output**

1

**Note:**The **Input/Ouput** format and **Example** given are used for system's internal purpose, and should be used by a user for **Expected Output** only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

op_manish | 189 |

ArpitGupta8 | 163 |

lone_walker | 160 |

Prince_Singh | 154 |

KshamaGupta | 154 |

saiujwal13083 | 728 |

ppsvikhyat17 | 712 |

generic_coder | 630 |

reclone_rk | 479 |

vt9941 | 479 |

blackshadows | 5448 |

Ibrahim Nash | 5438 |

akhayrutdinov | 5111 |

mb1973 | 4976 |

Quandray | 4752 |

Login to report an issue on this page.