-
Notifications
You must be signed in to change notification settings - Fork 14
/
Binary_tree_to_doubly_linked_list.cpp
61 lines (55 loc) · 1.4 KB
/
Binary_tree_to_doubly_linked_list.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
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
using namespace std;
struct BinaryTree {
int data;
BinaryTree* left, *right;
BinaryTree(int val = 0, BinaryTree* l = nullptr, BinaryTree* r = nullptr)
: data(val), left(l), right(r) {}
};
// @include
BinaryTree* convert_tree_to_doubly_list(BinaryTree* n) {
if (n == nullptr) {
return nullptr;
}
BinaryTree* L = convert_tree_to_doubly_list(n->left);
BinaryTree* R = convert_tree_to_doubly_list(n->right);
// Join L and n as a doubly linked list
BinaryTree* L_tail = nullptr;
if (L) {
L_tail = L->left;
L_tail->right = n, n->left = L_tail;
L_tail = n;
} else {
L = L_tail = n;
}
// Join L and R as a doubly linked list
BinaryTree* R_tail = nullptr;
if (R) {
R_tail = R->left;
L_tail->right = R, R->left = L_tail;
} else {
R_tail = L_tail;
}
R_tail->right = L, L->left = R_tail;
return L;
}
// @exclude
int main(int argc, char* argv[]) {
// 3
// 2 5
// 1 4 6
BinaryTree* root = new BinaryTree(3);
root->left = new BinaryTree(2);
root->left->left = new BinaryTree(1);
root->right = new BinaryTree(5);
root->right->left = new BinaryTree(4);
root->right->right = new BinaryTree(6);
// should output 1, 2, 3, 4, 5, 6
BinaryTree* head = convert_tree_to_doubly_list(root);
BinaryTree* temp = head;
do {
cout << temp->data << endl;
temp = temp->right;
} while (temp != head);
return 0;
}