Search topics...

Flatten Binary Tree to Linked List

medium
linked-listTime: O(n)Space: O(1)Frequency: 2

Given the root of a binary tree, flatten the tree into a linked list in-place following pre-order traversal.

Example 1:

Input:     1
          / \
         2   5
        / \   \
       3   4   6

Output: 1 -> 2 -> 3 -> 4 -> 5 -> 6
(Each node's left is null, right points to next in pre-order)

Example 2:

Input: root = []
Output: []
stacklinked-listrecursion