Left child, right sibling (2) P58904


Statement
 

pdf   zip

html

Write a program that reads binary trees of natural numbers, and for each one prints the postorder traversal of its general associated tree.

Input

Input starts with m, the number of trees that must be treated. The description of the m trees follow as is explained at the exercise : “”, with one exceptions: The number of nodes is not given, because you do not need to store the trees in any vector to solve this exercise. You can suppose that none of the given trees is empty, but that their right subtrees are empty.

Output

For each binary tree given, your program must print a line with the postorder traversal of the corresponding generated tree. Each element must be preceded by a space.

Public test cases
  • Input

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

    Output

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