Sum of trees P18593


Statement
 

pdf   zip

thehtml

We define the sum of two trees as the tree that is obtained adding the values of the nodes, overlapping the children of each subtree from the left to the right.

(To see an instance with the trees corresponding to the input-output instance, consult the pdf or ps version of this wording.)

Write a program that prints the preorder traversal of the sum of two given trees.

Input

Input consists of the description of two general trees of natural numbers, as is explained at the exercise .

Output

Your program must print a line with the preorder traversal of the sum of the two trees. Each element must be preceded of a space.

Public test cases
  • Input

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

    Output

     8 9 0 9 5 8 0 6 7 2 9 1 0
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Translator
    Carlos Molina
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    C++
    User solutions
    C++ Python