-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBT_2_DLL.cpp
49 lines (45 loc) · 1.06 KB
/
BT_2_DLL.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
44
45
46
47
48
49
/* Structure for tree and linked list
struct Node
{
int data;
struct Node* left;
struct Node* right;
Node(int x){
data = x;
left = right = NULL;
}
};
*/
// This function should return head to the DLL
class Solution
{
public:
void inorder(Node *root, vector<int> &ans)
{
if (root == NULL)
return;
inorder(root->left, ans);
ans.push_back(root->data);
inorder(root->right, ans);
}
Node *bToDLL(Node *root)
{
// code here
vector<int> ans;
inorder(root, ans);
Node *head = new Node(ans[0]);
head->left = NULL;
head->right = NULL;
int n = ans.size();
Node *point = head;
for (int i = 1; i < n; i++)
{
Node *node = new Node(ans[i]);
head->right = node;
node->left = head;
node->right = NULL;
head = node;
}
return point;
}
};