#K88222. Level Order Traversal of a Binary Tree
Level Order Traversal of a Binary Tree
Level Order Traversal of a Binary Tree
You are given a binary tree represented in level order. Your task is to perform a level order traversal of the tree and output the values of the nodes in the order they are visited. If the tree is empty, output an error message.
The binary tree is provided as a single line of input where the tokens are separated by spaces. Each token is either an integer or the string null
which represents an absent child node. For example, the input 1 2 3 4 5 6 7
represents the following binary tree:
1 / \ 2 3 / \ / \ 4 5 6 7
If the first token is null
, the tree is considered empty and you should output the error message "The tree is empty or null."
Note: In your code, if any mathematical expressions are included, make sure to wrap them in LaTeX format (for example, $$ expression $$
).
inputFormat
The input is given via standard input (stdin
) as a single line containing tokens separated by spaces. Each token represents a node in the binary tree in level order. A token may be an integer or null
indicating the absence of a node.
Examples:
1 2 3 4 5 6 7
1
1 2 null 3 null 4
(represents a left-heavy tree)null
(represents an empty tree)
outputFormat
If the tree is non-empty, output the level order traversal of the tree via standard output (stdout
) as a single line of space-separated integers. If the tree is empty, output the line:
The tree is empty or null.
1 2 3 4 5 6 7
1 2 3 4 5 6 7