I2F03. Level order traversal of a binary tree P99488


Statement
 

pdf   zip

thehtml

Your task is to write a program that reads a non empty binary tree of natural numbers, and prints its level order traversal (that is, from top to bottom and from left to right), with a line for each level.

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

Input

The input consists of the description of non empty a binary tree of natural numbers as it is explained at the exercise of the subject collection.

Output

Your program must print as many lines as levels, with the level order traversal of the tree. Follow the format of the instances.

Public test cases
  • Input

    10
    3 0 7 -1 4 -1 -1 2 -1 -1 5
    4 -1 -1 7 6 -1 1 -1 -1 -1
    

    Output

    level 1: 3
    level 2: 0 5
    level 3: 7 2 4 7
    level 4: 4 6
    level 5: 1
    
  • Input

    1
    7 -1 -1
    

    Output

    level 1: 7
    
  • Input

    4
    9 -1 8 7 -1 6 -1 -1 -1
    

    Output

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