File tree Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Original file line number Diff line number Diff line change
1
+ // https://www.geeksforgeeks.org/in-place-convert-a-given-binary-tree-to-doubly-linked-list/
2
+ #include < bits/stdc++.h>
3
+ using namespace std ;
4
+ struct Node {
5
+ int data;
6
+ Node *left;
7
+ Node *right;
8
+ };
9
+ Node * getnewnode (int data){
10
+ Node *newnode = new Node ();
11
+ newnode->data = data;
12
+ newnode->left = NULL ;
13
+ newnode->right = NULL ;
14
+ }
15
+
16
+ Node *convertToDLL (Node *root){
17
+ if (!root)
18
+ return root;
19
+ if (root->left ){
20
+ Node *l = convertToDLL (root->left );
21
+ for (;l->right ;l=l->right );
22
+ root->left = l;
23
+ l->right = root;
24
+ }
25
+ if (root->right ){
26
+ Node *r = convertToDLL (root->right );
27
+ for (;r->left ;r=r->left );
28
+ r->left = root;
29
+ root->right = r;
30
+ }
31
+ return root;
32
+ }
33
+
34
+
35
+ int main (){
36
+ Node * root = getnewnode (10 );
37
+ root->left = getnewnode (12 );
38
+ root->right = getnewnode (15 );
39
+ root->left ->left = getnewnode (25 );
40
+ root->left ->right = getnewnode (30 );
41
+ root->right ->left = getnewnode (36 );
42
+
43
+ Node *res = convertToDLL (root);
44
+ while (res->left )
45
+ res = res->left ;
46
+
47
+ while (res){
48
+ cout << res->data << " " ;
49
+ res = res->right ;
50
+ }
51
+ return 0 ;
52
+ }
You can’t perform that action at this time.
0 commit comments