-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryTreeToDLLSimple.cpp
More file actions
74 lines (64 loc) · 1.34 KB
/
Copy pathbinaryTreeToDLLSimple.cpp
File metadata and controls
74 lines (64 loc) · 1.34 KB
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
65
66
67
68
69
70
71
72
73
74
#include<iostream>
#include<queue>
#include<map>
#include<climits>
#include<stack>
#include<stdio.h>
using namespace std;
struct node{
int data;
struct node* left;
struct node* right;
};
struct node* newNode(int data){
struct node* node=new(struct node);
node->data=data;
node->left=node->right=NULL;
return node;
}
struct node* insert(struct node* node,int data){
if(node==NULL)
return (newNode(data));
if(data<=node->data)
node->left=insert(node->left,data);
else
node->right=insert(node->right,data);
return node;
}
node* binToList(node* root){
if(root==NULL)
return root;
if(root->left!=NULL){
node* left;
left=binToList(root->left);
for(;left->right!=NULL;left=left->right);
left->right=root;
root->left=left;
}
if(root->right!=NULL){
node* right;
right=binToList(root->right);
for(;right->left!=NULL;right=right->left);
root->right=right;
right->left=root;
}
return root;
}
node* convertToDLL(node* root){
if(root==NULL)
return root;
root=binToList(root);
for(;root->left!=NULL;root=root->left);
return root;
}
int main(){
struct node* root=NULL;
root=insert(root,4);
root=insert(root,2);
root=insert(root,5);
root=insert(root,1);
root=insert(root,3);
node* list=convertToDLL(root);
for(;list!=NULL;list=list->right)
cout<<list->data<<" ";
}