-
Notifications
You must be signed in to change notification settings - Fork 0
/
BTtoDLL.cpp
43 lines (40 loc) · 808 Bytes
/
BTtoDLL.cpp
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
#include<bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* left;
struct Node* right;
Node(int x){
data = x;
left = right = NULL;
}
};
void insertAtTail(vector<int> &v, Node* &head){
Node* ptr = head, *tail = ptr;
for(int &ele: v){
if(head == NULL){
ptr = new Node(ele);
head = ptr;
tail = ptr;
}
else {
tail = new Node(ele);
ptr->right = tail;
tail->left = ptr;
ptr = ptr->right;
}
}
}
void inorder(Node* &root, vector<int> &v){
if(!root) return;
inorder(root->left, v);
v.push_back(root->data);
inorder(root->right, v);
}
Node* bToDLL(Node* root) {
vector<int> in;
inorder(root, in);
Node* head = NULL;
insertAtTail(in, head);
return head;
}