Skip to content

Commit 199251b

Browse files
committed
add code convert BT to DLL
1 parent 75eb368 commit 199251b

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

a.out

-4.07 KB
Binary file not shown.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy