AlgoUniversity
  • Go Back
Discussion
Overlapping Trees :

Author

Nikunj Nawal

Difficulty Level : Easy

Submissions : 66

Asked In :

Marks :10

: 2 | : 4

Given two binary trees, overlap $$$Tree-2$$$ over $$$Tree-1$$$.

Note: The merging process must start from the root nodes of both trees.

Input

The first line of input contains two integers $$$n$$$ and $$$m$$$ — the length of two arrays representing $$$Tree-1$$$ and $$$Tree-2$$$. $$$(1 \le n,m \le 10^5)$$$

The second input line contains $$$n$$$ single space-separated integer values representing $$$Tree-1$$$. $$$(0 \le values \le 10^6)$$$

The second input line contains $$$m$$$ single space-separated integer values representing $$$Tree-2$$$. $$$(0 \le values \le 10^6)$$$

$$$0$$$ node value represents a null node.

It is guaranteed that the tree input given will be valid.

Output

An array represents the tree formed after overlapping. The output array should be in the same format as the input, performing a level-order tree traversal.

Examples

Input
3 4
1 2 3
4 5 6 7
Output
4 5 6 7 0 
Input
5 6
1 0 2 0 3
1 2 0 3 0 4
Output
1 2 2 3 0 0 3 4 0 
Input
5 6
1 2 3 0 4
5 6 0 0 9 10
Output
5 6 3 0 9 
Input
7 7
1 0 2 0 3 0 4
5 6 0 7 0 8 0
Output
5 6 2 7 0 0 3 8 0 0 4 

Note

A tree represented by array $$$[5,6,0,0,9]$$$ is 5 as root with the left child as 6 and 6 has right child as 9.

You need to login to view your submissions.

You need to login to view all submissions.

Loading...

Result : Executed

Loading...

Feel something is wrong with the test cases?

Result : Accepted

Test Cases :

You need to Log In
We're glad that you want to attempt this problem!

But to Run or Submit the Problem, you need to Log In.

Continue to Log In
Challenge Submitted!

Your challenge has been submitted successfully.

You will get a response soon via WhatsApp or Email.

Challenge
Facing issue while trying to solve the problem! Don't worry, we got you covered!

Do let us know your issue.

Looks good!
Please enter your issue / feedback.

How do we get in touch with you?
Looks good!
Please enter your phone no.
Looks good!
Please enter your email address.