-
Notifications
You must be signed in to change notification settings - Fork 13
/
CS_50_BST_ToSsortedDLL.cpp
61 lines (54 loc) · 1.23 KB
/
CS_50_BST_ToSsortedDLL.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 <bits/stdc++.h>
using namespace std;
template <typename T>
class TreeNode
{
public:
T data;
TreeNode<T> *left;
TreeNode<T> *right;
TreeNode(T data)
{
this->data = data;
left = NULL;
right = NULL;
}
};
void solve(TreeNode<int> *root, TreeNode<int>* &head){
if(!root) return;
// RNL
// R
solve(root->right, head);
// N
root->right = head; // attach root's right to head
if(head != NULL) // if head is not null then attach head's left to root as this is a doubly linked list
head->left = root;
head = root; // make root as the new head
// L
solve(root->left, head);
}
TreeNode<int> *bstToSortedDLL(TreeNode<int> *root)
{
TreeNode<int> *head = NULL;
solve(root, head);
return head;
}
void printList(TreeNode<int> *head)
{
while (head)
{
cout << head->data << " ";
head = head->right;
}
}
int main()
{
TreeNode<int> *root = new TreeNode<int>(10);
root->left = new TreeNode<int>(5);
root->right = new TreeNode<int>(20);
root->right->left = new TreeNode<int>(30);
root->right->right = new TreeNode<int>(35);
TreeNode<int> *head = bstToSortedDLL(root);
printList(head);
return 0;
}