-
Notifications
You must be signed in to change notification settings - Fork 0
/
426 Convert Binary Search Tree to Sorted Doubly Linked List
64 lines (56 loc) · 1.41 KB
/
426 Convert Binary Search Tree to Sorted Doubly Linked List
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
63
64
#include <iostream>
using namespace std;
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
void inorder(TreeNode *root, TreeNode*& leftLast, TreeNode*& head) {
if (!root) return;
inorder(root->left, leftLast, head);
if (!head) {
head = root;
leftLast = root;
} else {
leftLast->right = root;
root->left = leftLast;
leftLast = root;
}
inorder(root->right, leftLast, head);
}
TreeNode* convert(TreeNode *root) {
if (!root) return NULL;
TreeNode* head = NULL;
TreeNode* leftLast = NULL;
inorder(root, leftLast, head);
head->left = leftLast;
leftLast->right = head;
return head;
}
// Display Circular Link List
void displayCList(TreeNode *head)
{
cout << "Circular Linked List is :\n";
TreeNode *itr = head;
do
{
cout << itr->val <<" ";
itr = itr->right;
} while (head!=itr);
cout << "\n";
}
// Driver Program to test above function
int main()
{
TreeNode *root = new TreeNode(10);
root->left = new TreeNode(12);
root->right = new TreeNode(15);
root->left->left = new TreeNode(25);
root->left->right = new TreeNode(30);
root->right->left = new TreeNode(36);
TreeNode *head = convert(root);
displayCList(head);
return 0;
}