-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
Binary Tree to DLL | ||
Hard Accuracy: 41.34% Submissions: 74409 Points: 8 | ||
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. | ||
TreeToList | ||
Example 1: | ||
Input: | ||
1 | ||
/ \ | ||
3 2 | ||
Output: | ||
3 1 2 | ||
2 1 3 | ||
Explanation: DLL would be 3<=>1<=>2 | ||
*/ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
#define ff first | ||
#define ss second | ||
// #define int long long | ||
#define pb push_back | ||
#define mp make_pair | ||
#define pii pair<int, int> | ||
#define vi vector<int> | ||
#define mii map<int, int> | ||
#define pqb priority_queue<int> //max heap | ||
#define pqs priority_queue<int, vi, greater<int>> //min heap | ||
#define setbits(x) __builtin_popcountll(x) //gives number of setbits | ||
#define zrobits(x) __builtin_ctzll(x) //gives number of 0 bits | ||
#define mod 1000000007 // 1e9+7 ans%mod | ||
#define inf 1e18 | ||
#define ps(x, y) fixed << setprecision(y) << x | ||
#define mk(arr, n, type) type *arr = new type[n]; //dynamic array | ||
#define w(x) \ | ||
int x; \ | ||
cin >> x; \ | ||
while (x--) //test case | ||
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //random shuffle | ||
|
||
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; //policy based ds | ||
|
||
#define deb(x) cout << #x << " " << x << endl; //for debugging | ||
#define fo(i, n) for (i = 0; i < n; i++) | ||
#define fo1(i, n) for (i = 1; i <= n; i++) | ||
#define Fo(i, k, n) for (i = k; i < n; i++) | ||
#define vii vector<pii> | ||
#define rep(i, a, b) for (int i = a; i < b; i++) | ||
|
||
#define vi vector<int> | ||
#define vvi vector<vi> | ||
|
||
/* | ||
struct Node | ||
{ | ||
int data; | ||
struct Node *left; //pointer pointing towards left child | ||
struct Node *right; //pointer for right child | ||
Node(int val) //constructor taking data | ||
{ | ||
data = val; | ||
left = NULL; //initially | ||
right = NULL; | ||
} | ||
}; | ||
*/ | ||
struct Node | ||
{ | ||
int data; | ||
struct Node *left; | ||
struct Node *right; | ||
|
||
Node(int x) | ||
{ | ||
data = x; | ||
left = right = NULL; | ||
} | ||
}; | ||
class Solution | ||
{ | ||
public: | ||
void solve(Node *root, Node *&head, Node *&prev, int &f) | ||
{ | ||
if (!root) | ||
{ | ||
return; | ||
} | ||
solve(root->left, head, prev, f); //inorder trav left rec | ||
if (f == 0) | ||
{ //when recieving first node | ||
f = 1; | ||
head = root; //we point both head and prev to root | ||
prev = root; | ||
} | ||
else | ||
{ //when recieving 2nd to n-1 nodes head does not change | ||
prev->right = root; | ||
prev->right->left = prev; | ||
prev = prev->right; | ||
} | ||
solve(root->right, head, prev, f); //right recursion | ||
} | ||
//Function to convert binary tree to doubly linked list and return it. | ||
Node *bToDLL(Node *root) | ||
{ | ||
// your code here | ||
Node *head = NULL; | ||
Node *prev = NULL; | ||
int f = 0; | ||
solve(root, head, prev, f); | ||
return head; | ||
} | ||
}; | ||
|
||
int main() | ||
{ | ||
|
||
return 0; | ||
} |