#K33912. Preorder Traversal of a Nested List Binary Tree
Preorder Traversal of a Nested List Binary Tree
Preorder Traversal of a Nested List Binary Tree
Given a binary tree in the form of a nested list, your task is to perform a preorder traversal on the tree. A binary tree is defined recursively as follows: $$T=\text{null} \quad \text{or} \quad T=[v, L, R]$$ where \(v\) is an integer value, and \(L\) and \(R\) are the left and right subtrees respectively.
The input will be provided as a single line, representing the tree in this nested list format. Your program should output the values of the nodes in the order they are visited (i.e. root, left subtree, right subtree), separated by spaces. If the tree is empty, output an empty line.
inputFormat
The input is a single line read from standard input, representing the binary tree. The tree is given in the following format: null
represents an empty tree; otherwise, it is given as [value, left, right]
, where left
and right
are either null
or another nested list representing a subtree.
outputFormat
Output the preorder traversal of the binary tree. Print the node values in one line separated by a single space. If the tree is empty, output an empty line.
## samplenull