


The merging process must start from the root nodes of both trees.
‘ROOT1’ = [1, 2, -1, -1, 3, -1, -1] ‘ROOT2’ = [3, -1, -1].
The final tree would look like : [3, 2, -1, -1, 3, -1, -1], and the output will be printed in a pre-order way: 4 2 3.

The first line of input contains an integer ‘T’ denoting the number of test cases.
In the next 2*T lines, the first line of each test case contains space-separated integers denoting the nodes of the first binary tree, where -1 indicates that the NULL pointer has been appointed to the previous node
The second line of each test case contains space-separated integers denoting the nodes of the second binary tree, where -1 indicates that the NULL pointer has been appointed to the previous node.
The input is given in a preorder way, that is, the node then left subtree, and then right subtree as shown in the example.
For each test case, return the resultant binary tree in a pre-order way.
The output of each test case will be printed 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 <= 5
1 <= N <= 10^3
-10^3 <= DATA <= 10^3
Time Limit: 1 second
The main idea is to use recursion to traverse the tree in a Pre-order way. If any of the nodes is null, return the other node, and if both are NULL, return NULL.
The main idea is to use the stack to traverse the tree instead of using recursion.
We start by pushing the root nodes of both the trees onto the stack.