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:

tree = <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

Title

Script

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Contrary to popular belief, Lorem Ipsum is not simply random text.

  >>> a = [1, 2, 3]
  >>> a[-1]
  3

Get premium for instant access to all content and solutions

Upgrade