Flatten Binary Tree to Linked List

Given a binary tree, return a linked list that is a "flattened" version of the tree.

The linked list still uses the same nodes as a normal binary tree, only the left subtree is always empty, and the right subtree always points to the next element in the linked list (or the empty tree).

The flattened tree represents the pre-order traversal of the tree.

Input

  • tree: the binary tree to be flattened.

Output

A tree representing the flattened binary tree.

Examples

Example 1:

Input:

1tree = <See explanation>

Output: <See explanation>

Explanation:

Input tree:

Flattened tree:

Note that this uses the binary tree structure to represent the linked list.

Try it yourself

Solution

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro