|
| 1 | +--- |
| 2 | +id: binary-tree-to-dll |
| 3 | +title: Binary Tree to DLL |
| 4 | +sidebar_label: Binary Tree to DLL |
| 5 | + |
| 6 | +tags: |
| 7 | +- Binary tree |
| 8 | +- DLL |
| 9 | + |
| 10 | +description: "This is a solution to the Binary Tree to DLL problem on GeeksForGeeks." |
| 11 | +--- |
| 12 | + |
| 13 | +## Problem Description |
| 14 | +Given a Binary Tree (BT), convert it to a Doubly Linked List(DLL) In-Place. The left and right pointers in nodes are to be used as previous and next pointers respectively in converted DLL. The order of nodes in DLL must be same as Inorder of the given Binary Tree. The first node of Inorder traversal (leftmost node in BT) must be the head node of the DLL. |
| 15 | +Note: H is the height of the tree and this space is used implicitly for the recursion stack. |
| 16 | + |
| 17 | +### Examples |
| 18 | + |
| 19 | +**Example 1:** |
| 20 | +``` |
| 21 | +Input: |
| 22 | + 1 |
| 23 | + / \ |
| 24 | + 3 2 |
| 25 | +
|
| 26 | +Output: |
| 27 | +3 1 2 |
| 28 | +2 1 3 |
| 29 | +
|
| 30 | +Explanation: Explanation: DLL would be 3<=>1<=>2 |
| 31 | +``` |
| 32 | + |
| 33 | +**Example 2:** |
| 34 | +``` |
| 35 | +Input: |
| 36 | + 10 |
| 37 | + / \ |
| 38 | + 20 30 |
| 39 | + / \ |
| 40 | + 40 60 |
| 41 | +
|
| 42 | +Output: |
| 43 | +40 20 60 10 30 |
| 44 | +30 10 60 20 40 |
| 45 | +
|
| 46 | +Explanation: |
| 47 | +DLL would be |
| 48 | +40<=>20<=>60<=>10<=>30. |
| 49 | +``` |
| 50 | + |
| 51 | + |
| 52 | +### Constraints |
| 53 | +- `1 ≤ Number of nodes ≤ 10^5` |
| 54 | +- `0 ≤ Data of a node ≤ 10^5` |
| 55 | + |
| 56 | + |
| 57 | +## Solution for Binary Tree to DLL |
| 58 | +### Approach |
| 59 | +#### Brute Force |
| 60 | +- **Inorder Traversal**: Perform an inorder traversal of the binary tree to collect nodes in the order they would appear in the DLL. |
| 61 | +- **Construct DLL**: As you traverse the tree in inorder |
| 62 | + - Keep track of the previously visited node. |
| 63 | + - Adjust pointers (`left` and `right`) of each node to convert it into a DLL node. |
| 64 | + - Update the head of the DLL once you reach the leftmost node (first node in inorder traversal). |
| 65 | + |
| 66 | +**Implementation:** |
| 67 | +```cpp |
| 68 | +// Definition for a binary tree node. |
| 69 | +struct TreeNode { |
| 70 | + int val; |
| 71 | + TreeNode* left; |
| 72 | + TreeNode* right; |
| 73 | + TreeNode(int x) : val(x), left(NULL), right(NULL) {} |
| 74 | +}; |
| 75 | + |
| 76 | +class Solution { |
| 77 | +public: |
| 78 | + TreeNode* treeToDoublyList(TreeNode* root) { |
| 79 | + if (root == nullptr) |
| 80 | + return nullptr; |
| 81 | + |
| 82 | + TreeNode* head = nullptr; // Head of the DLL |
| 83 | + TreeNode* prev = nullptr; // Previous node in inorder traversal |
| 84 | + |
| 85 | + inorder(root, prev, head); |
| 86 | + |
| 87 | + // Connect head and tail for circular DLL |
| 88 | + head->left = prev; |
| 89 | + prev->right = head; |
| 90 | + |
| 91 | + return head; |
| 92 | + } |
| 93 | + |
| 94 | + void inorder(TreeNode* node, TreeNode*& prev, TreeNode*& head) { |
| 95 | + if (node == nullptr) |
| 96 | + return; |
| 97 | + |
| 98 | + // Recursively traverse left subtree |
| 99 | + inorder(node->left, prev, head); |
| 100 | + |
| 101 | + // Process current node |
| 102 | + if (prev == nullptr) { |
| 103 | + // This is the leftmost node (head of DLL) |
| 104 | + head = node; |
| 105 | + } else { |
| 106 | + // Connect previous node with current node |
| 107 | + prev->right = node; |
| 108 | + node->left = prev; |
| 109 | + } |
| 110 | + prev = node; // Update prev to current node |
| 111 | + |
| 112 | + // Recursively traverse right subtree |
| 113 | + inorder(node->right, prev, head); |
| 114 | + } |
| 115 | +}; |
| 116 | +``` |
| 117 | +
|
| 118 | +
|
| 119 | +#### Optimized Approach |
| 120 | +- **Algorithm**: |
| 121 | + - Use a recursive approach to perform an inorder traversal while adjusting the pointers to form a doubly linked list on the fly. |
| 122 | + - Maintain a reference to the previously processed node to set the `left` and `right` pointers correctly. |
| 123 | +- **Steps**: |
| 124 | + - Initialize `prev` as `None` and `head` as `None` to keep track of the previous node and the head of the DLL, respectively. |
| 125 | + - Define a recursive function `convert_to_dll` that: |
| 126 | + - Recursively converts the left subtree. |
| 127 | + - Adjusts the pointers of the current node based on the `prev` node. |
| 128 | + - Updates `prev` to the current node. |
| 129 | + - Recursively converts the right subtree. |
| 130 | + - Call the recursive function with the root node. |
| 131 | + - Return the `head` of the DLL. |
| 132 | +
|
| 133 | +**Complexity:** |
| 134 | +- Time Complexity: O(N), where N is the number of nodes in the binary tree. Each node is visited exactly once. |
| 135 | +- Space Complexity: O(H), where H is the height of the binary tree. This is the space required for the recursion stack. |
| 136 | +
|
| 137 | +**Corner Cases:** |
| 138 | +- The binary tree is empty (i.e., the root is `None`). |
| 139 | +- The binary tree has only one node. |
| 140 | +
|
| 141 | +
|
| 142 | +## Code in Different Languages |
| 143 | +
|
| 144 | +<Tabs> |
| 145 | +
|
| 146 | +<TabItem value="Python" label="Python"> |
| 147 | +<SolutionAuthor name="@vansh-codes" /> |
| 148 | +
|
| 149 | + ```python |
| 150 | + class Solution: |
| 151 | + def __init__(self): |
| 152 | + self.prev = None |
| 153 | + self.head = None |
| 154 | + |
| 155 | + |
| 156 | + def bToDLL(self, root): |
| 157 | + if not root: |
| 158 | + return None |
| 159 | + |
| 160 | + def inorder(node): |
| 161 | + if not node: |
| 162 | + return |
| 163 | +
|
| 164 | + inorder(node.left) |
| 165 | +
|
| 166 | + if self.prev: |
| 167 | + self.prev.right = node |
| 168 | + node.left = self.prev |
| 169 | + else: |
| 170 | + self.head = node |
| 171 | + |
| 172 | + self.prev = node |
| 173 | +
|
| 174 | + inorder(node.right) |
| 175 | + |
| 176 | + inorder(root) |
| 177 | + return self.head |
| 178 | + ``` |
| 179 | +
|
| 180 | +</TabItem> |
| 181 | +
|
| 182 | +<TabItem value="Java" label="Java"> |
| 183 | +<SolutionAuthor name="@vansh-codes" /> |
| 184 | +
|
| 185 | + ``` |
| 186 | + class Solution |
| 187 | + { |
| 188 | + //Function to convert binary tree to doubly linked list and return it. |
| 189 | + Node prev = null; |
| 190 | + Node head = null; |
| 191 | + |
| 192 | + // Function to convert binary tree to doubly linked list and return it. |
| 193 | + public Node bToDLL(Node root) { |
| 194 | + if (root == null) { |
| 195 | + return null; |
| 196 | + } |
| 197 | + |
| 198 | + inorder(root); |
| 199 | + return head; |
| 200 | + } |
| 201 | + |
| 202 | + private void inorder(Node node) { |
| 203 | + if (node == null) { |
| 204 | + return; |
| 205 | + } |
| 206 | + |
| 207 | + inorder(node.left); |
| 208 | + |
| 209 | + if (prev != null) { |
| 210 | + prev.right = node; |
| 211 | + node.left = prev; |
| 212 | + } else { |
| 213 | + head = node; |
| 214 | + } |
| 215 | + |
| 216 | + prev = node; |
| 217 | + |
| 218 | + inorder(node.right); |
| 219 | + } |
| 220 | + } |
| 221 | + ``` |
| 222 | + |
| 223 | +</TabItem> |
| 224 | + |
| 225 | +<TabItem value="C++" label="C++"> |
| 226 | +<SolutionAuthor name="@vansh-codes" /> |
| 227 | + |
| 228 | + ```cpp |
| 229 | + class Solution { |
| 230 | + public: |
| 231 | + Node* prev = nullptr; |
| 232 | + Node* head = nullptr; |
| 233 | + |
| 234 | + // Function to convert binary tree to doubly linked list and return it. |
| 235 | + Node* bToDLL(Node* root) { |
| 236 | + if (root == nullptr) { |
| 237 | + return nullptr; |
| 238 | + } |
| 239 | + |
| 240 | + inorder(root); |
| 241 | + return head; |
| 242 | + } |
| 243 | + |
| 244 | + private: |
| 245 | + void inorder(Node* node) { |
| 246 | + if (node == nullptr) { |
| 247 | + return; |
| 248 | + } |
| 249 | + |
| 250 | + inorder(node->left); |
| 251 | + |
| 252 | + if (prev != nullptr) { |
| 253 | + prev->right = node; |
| 254 | + node->left = prev; |
| 255 | + } else { |
| 256 | + head = node; |
| 257 | + } |
| 258 | + |
| 259 | + prev = node; |
| 260 | + |
| 261 | + inorder(node->right); |
| 262 | + } |
| 263 | + }; |
| 264 | + ``` |
| 265 | + |
| 266 | +</TabItem> |
| 267 | +</Tabs> |
| 268 | + |
| 269 | +## References |
| 270 | + |
| 271 | +- **LeetCode Problem**: [Binary Tree to DLL](https://www.geeksforgeeks.org/problems/binary-tree-to-dll/1) |
0 commit comments