forked from Midway91/HactoberFest2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Convert Binary to DLL
62 lines (48 loc) · 1 KB
/
Convert Binary to DLL
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
#include <bits/stdc++.h>
struct Node {
int data;
Node *left, *right;
};
Node* newNode(int data)
{
Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return node;
}
void BToDLL(Node* root, Node*& head)
{
if (root == NULL)
return;
BToDLL(root->right, head);
root->right = head;
if (head != NULL)
head->left = root;
head = root;
BToDLL(root->left, head);
}
void printList(Node* head)
{
printf("Extracted Double Linked list is:\n");
while (head) {
printf("%d ", head->data);
head = head->right;
}
}
int main()
{
Node* root = newNode(5);
root->left = newNode(3);
root->right = newNode(6);
root->left->left = newNode(1);
root->left->right = newNode(4);
root->right->right = newNode(8);
root->left->left->left = newNode(0);
root->left->left->right = newNode(2);
root->right->right->left = newNode(7);
root->right->right->right = newNode(9);
Node* head = NULL;
BToDLL(root, head);
printList(head);
return 0;
}