Trees and parenthesis P67136


Statement
 

pdf   zip

thehtml

Write a program that reads the shape of various general trees and prints the parenthesis associated to each tree, as it is shown at the instance.

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 two exceptions: The values are not given, because the content of the nodes here is not important. The number of nodes is neither given, because you do not need to store the trees in any vector to solve this exercise.

Output

Your program must print a line for each tree of the input, with its associated parentheses.

Public test cases
  • Input

    9
    
    1 0
    2 0 0
    1 1 0
    0
    3 0 0 0
    2 1 0 0
    2 0 1 0
    1 2 0 0
    1 1 1 0
    

    Output

    ()
    ()()
    (())
    
    ()()()
    (())()
    ()(())
    (()())
    ((()))
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Translator
    Carlos Molina
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    C++
    User solutions
    C++