114 Flatten Binary Tree to Linked List

114. Flatten Binary Tree to Linked List

1. Question

Given a binary tree, flatten it to a linked list in-place.

For example, Given

         1
        / \
       2   5
      / \   \
     3   4   6

The flattened tree should look like:

   1
    \
     2
      \
       3
        \
         4
          \
           5
            \
             6

2. Implementation

(1) Morris Tree Traversal

(2) DFS recursion

(3) DFS iteration

3. Time & Space Complexity

Morris Tree Traversal: 时间复杂度O(n), 空间复杂度O(1)

DFS recursion: 时间复杂度O(n), 空间复杂度O(n)

DFS iteration: 时间复杂度O(n), 空间复杂度(n)

Last updated

Was this helpful?