# 114. Flatten Binary Tree to Linked List

## Problem Statement

\
Given the `root` of a binary tree, flatten the tree into a "linked list":

* The "linked list" should use the same `TreeNode` class where the `right` child pointer points to the next node in the list and the `left` child pointer is always `null`.
* The "linked list" should be in the same order as a [**pre-order traversal**](https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR) of the binary tree.

&#x20;

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg)

<pre><code><strong>Input: root = [1,2,5,3,4,null,6]
</strong><strong>Output: [1,null,2,null,3,null,4,null,5,null,6]
</strong></code></pre>

**Example 2:**

<pre><code><strong>Input: root = []
</strong><strong>Output: []
</strong></code></pre>

**Example 3:**

<pre><code><strong>Input: root = [0]
</strong><strong>Output: [0]
</strong></code></pre>

&#x20;

**Constraints:**

* The number of nodes in the tree is in the range `[0, 2000]`.
* `-100 <= Node.val <= 100`

&#x20;

**Follow up:** Can you flatten the tree in-place (with `O(1)` extra space)?

## Intuition

```
Approach 1:
Using recursion, maintain a prev node to attach left tree to right part
And right part to null

Approach 2:
Iterative, same as above

Approach 3: 
Morris traversal

```

### Links

<https://leetcode.com/problems/flatten-binary-tree-to-linked-list/description/>

### Video Links

<https://www.youtube.com/watch?v=sWf7k1x9XR4&t=587s&ab_channel=takeUforward>

### Approach 1:

```
Recursion
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    TreeNode *prev = nullptr;
    void flatten(TreeNode* root) {
        if(root == nullptr)
            return ;

        flatten(root->right);
        flatten(root->left);

        root->right = prev;
        root->left = nullptr;
        prev = root;
    }
};
```

{% endcode %}

### Approach 2:

```
Iterative
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    
    void flatten(TreeNode* root) {
        if(root == NULL) return;
        stack<TreeNode*> st;
        st.push(root);

        while(!st.empty()){
            TreeNode* curr = st.top();
            st.pop();

            if(curr->right)
                st.push(curr->right);
            if(curr->left)
                st.push(curr->left);

            if(!st.empty())
                curr->right = st.top();

            curr->left = nullptr;
        }
    }
};
```

{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 4:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Similar Problems

###


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://coding-9.gitbook.io/untitled/trees/binary-tree/hard/114.-flatten-binary-tree-to-linked-list.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
