-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path08_TreeTransformations.cpp
More file actions
274 lines (222 loc) · 8.08 KB
/
Copy path08_TreeTransformations.cpp
File metadata and controls
274 lines (222 loc) · 8.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
================================================================================
TREE TRANSFORMATIONS
================================================================================
*/
#include <bits/stdc++.h>
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
/*
PROBLEM 1: Flatten Binary Tree to Linked List (LeetCode 114) ⭐ GOOGLE FAVORITE
───────────────────────────────────────────────────────────────────────────────
Flatten to right-skewed tree in preorder.
Time: O(n) | Space: O(1) with Morris
*/
void flatten(TreeNode* root) {
TreeNode* curr = root;
while (curr) {
if (curr->left) {
// Find rightmost node of left subtree
TreeNode* pred = curr->left;
while (pred->right) {
pred = pred->right;
}
// Connect to right subtree
pred->right = curr->right;
curr->right = curr->left;
curr->left = nullptr;
}
curr = curr->right;
}
}
/*
PROBLEM 2: Invert Binary Tree (LeetCode 226) ⭐ CLASSIC
──────────────────────────────────────────────────────
Mirror the tree.
*/
TreeNode* invertTree(TreeNode* root) {
if (!root) return nullptr;
swap(root->left, root->right);
invertTree(root->left);
invertTree(root->right);
return root;
}
/*
PROBLEM 3: Convert BST to Greater Tree (LeetCode 538)
─────────────────────────────────────────────────────
Each node's value = sum of all greater values + original.
Reverse inorder traversal.
*/
TreeNode* convertBST(TreeNode* root) {
int sum = 0;
function<void(TreeNode*)> reverseInorder = [&](TreeNode* node) {
if (!node) return;
reverseInorder(node->right);
sum += node->val;
node->val = sum;
reverseInorder(node->left);
};
reverseInorder(root);
return root;
}
/*
PROBLEM 4: Binary Tree to Linked List (Inorder)
───────────────────────────────────────────────
*/
TreeNode* treeToList(TreeNode* root) {
TreeNode dummy(0);
TreeNode* prev = &dummy;
function<void(TreeNode*)> inorder = [&](TreeNode* node) {
if (!node) return;
inorder(node->left);
prev->right = node;
node->left = prev;
prev = node;
inorder(node->right);
};
inorder(root);
if (dummy.right) dummy.right->left = nullptr;
return dummy.right;
}
/*
PROBLEM 5: Populating Next Right Pointers (LeetCode 116) ⭐
──────────────────────────────────────────────────────────
Connect nodes at same level (perfect binary tree).
Time: O(n) | Space: O(1)
*/
struct Node {
int val;
Node* left;
Node* right;
Node* next;
Node(int x) : val(x), left(nullptr), right(nullptr), next(nullptr) {}
};
Node* connect(Node* root) {
if (!root) return nullptr;
Node* leftmost = root;
while (leftmost->left) {
Node* curr = leftmost;
while (curr) {
curr->left->next = curr->right;
if (curr->next) {
curr->right->next = curr->next->left;
}
curr = curr->next;
}
leftmost = leftmost->left;
}
return root;
}
// For any binary tree (LeetCode 117)
Node* connectAny(Node* root) {
Node* curr = root;
while (curr) {
Node dummy(0);
Node* prev = &dummy;
while (curr) {
if (curr->left) {
prev->next = curr->left;
prev = prev->next;
}
if (curr->right) {
prev->next = curr->right;
prev = prev->next;
}
curr = curr->next;
}
curr = dummy.next;
}
return root;
}
/*
PROBLEM 6: Balance BST (LeetCode 1382)
─────────────────────────────────────
Convert to sorted array, rebuild balanced.
*/
TreeNode* balanceBST(TreeNode* root) {
vector<int> sorted;
function<void(TreeNode*)> inorder = [&](TreeNode* node) {
if (!node) return;
inorder(node->left);
sorted.push_back(node->val);
inorder(node->right);
};
inorder(root);
function<TreeNode*(int, int)> build = [&](int l, int r) -> TreeNode* {
if (l > r) return nullptr;
int mid = l + (r - l) / 2;
TreeNode* node = new TreeNode(sorted[mid]);
node->left = build(l, mid - 1);
node->right = build(mid + 1, r);
return node;
};
return build(0, sorted.size() - 1);
}
/*
PROBLEM 7: Delete Leaves With Given Value (LeetCode 1325)
─────────────────────────────────────────────────────────
Recursively delete leaves with target value.
*/
TreeNode* removeLeafNodes(TreeNode* root, int target) {
if (!root) return nullptr;
root->left = removeLeafNodes(root->left, target);
root->right = removeLeafNodes(root->right, target);
if (!root->left && !root->right && root->val == target) {
return nullptr;
}
return root;
}
/*
PROBLEM 8: Linked List in Binary Tree (LeetCode 1367)
─────────────────────────────────────────────────────
Check if linked list is a downward path in tree.
*/
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
};
bool isSubPath(ListNode* head, TreeNode* root) {
function<bool(ListNode*, TreeNode*)> match = [&](ListNode* l, TreeNode* t) -> bool {
if (!l) return true;
if (!t) return false;
if (l->val != t->val) return false;
return match(l->next, t->left) || match(l->next, t->right);
};
if (!root) return false;
return match(head, root) || isSubPath(head, root->left) || isSubPath(head, root->right);
}
int main() {
cout << "=== Tree Transformations ===\n\n";
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(5);
root->left->left = new TreeNode(3);
root->left->right = new TreeNode(4);
root->right->right = new TreeNode(6);
flatten(root);
cout << "Flattened (preorder): ";
while (root) {
cout << root->val << " ";
root = root->right;
}
cout << "\n";
return 0;
}
/*
SUMMARY:
+───────────────────────────+────────────────────────────────────────+
| Transformation | Technique |
+───────────────────────────+────────────────────────────────────────+
| Flatten to List | Morris-like: connect left to right |
| Invert | Swap children recursively |
| BST to Greater Tree | Reverse inorder with running sum |
| Next Right Pointers | Level-by-level using next pointers |
| Balance BST | Inorder to array, rebuild balanced |
+───────────────────────────+────────────────────────────────────────+
*/