#K1421. Flatten Binary Tree to Linked List

    ID: 24084 Type: Default 1000ms 256MiB

Flatten Binary Tree to Linked List

Flatten Binary Tree to Linked List

You are given a binary tree represented in level order. Your task is to flatten the binary tree into a singly linked list in-place using pre-order traversal. In the resulting list, every node's left pointer must be set to null, and its right pointer should point to the next node in the pre-order sequence.

The tree is given as a sequence of space‐separated tokens. Each token is either an integer (representing a node's value) or the literal null (representing a missing node). You need to reconstruct the tree from this representation, apply the flattening procedure, and then output the values of the nodes by following the right pointers, separated by a single space.

Note: If the tree is empty, output nothing.

inputFormat

The input consists of a single line containing space-separated tokens representing the binary tree in level order. Each token is either an integer or the string "null" indicating an absent (missing) node.

outputFormat

Print the values of the flattened tree's nodes in order from the root using the right child pointers, separated by a single space. If the tree is empty, do not output anything.## sample

1
1

</p>