#K64247. Flatten Binary Tree to Linked List

    ID: 31933 Type: Default 1000ms 256MiB

Flatten Binary Tree to Linked List

Flatten Binary Tree to Linked List

You are given the root of a binary tree. Your task is to flatten the binary tree into a "linked list" in-place. After flattening, every node's left child should be null and the right child should point to the next node in a pre-order traversal of the tree.

Note: The binary tree is provided as a level-order traversal where the keyword null represents a missing node.

inputFormat

The input consists of a single line containing space-separated tokens which represent the level-order traversal of the binary tree. Each token is either an integer or the string null (without quotes) representing a missing child. For example:

1 2 5 3 4 null 6

outputFormat

Output a single line with the values of the flattened tree (following the right pointers) separated by a single space. For the above example, the output should be:

1 2 3 4 5 6

## sample
1 2 5 3 4 null 6
1 2 3 4 5 6