Skip to content

Commit dbe7caf

Browse files
Create convert_ binary_tree_to_doubly_linked_list.cpp
1 parent a963bb2 commit dbe7caf

File tree

1 file changed

+198
-0
lines changed

1 file changed

+198
-0
lines changed
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
// { Driver Code Starts
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
// Tree Node
6+
struct Node
7+
{
8+
int data;
9+
Node* left;
10+
Node* right;
11+
};
12+
13+
// Utility function to create a new Tree Node
14+
Node* newNode(int val)
15+
{
16+
Node* temp = new Node;
17+
temp->data = val;
18+
temp->left = NULL;
19+
temp->right = NULL;
20+
21+
return temp;
22+
}
23+
24+
// Function to Build Tree
25+
Node* buildTree(string str)
26+
{
27+
// Corner Case
28+
if(str.length() == 0 || str[0] == 'N')
29+
return NULL;
30+
31+
// Creating vector of strings from input
32+
// string after spliting by space
33+
vector<string> ip;
34+
35+
istringstream iss(str);
36+
for(string str; iss >> str; )
37+
ip.push_back(str);
38+
39+
// Create the root of the tree
40+
Node* root = newNode(stoi(ip[0]));
41+
42+
// Push the root to the queue
43+
queue<Node*> queue;
44+
queue.push(root);
45+
46+
// Starting from the second element
47+
int i = 1;
48+
while(!queue.empty() && i < ip.size()) {
49+
50+
// Get and remove the front of the queue
51+
Node* currNode = queue.front();
52+
queue.pop();
53+
54+
// Get the current node's value from the string
55+
string currVal = ip[i];
56+
57+
// If the left child is not null
58+
if(currVal != "N") {
59+
60+
// Create the left child for the current node
61+
currNode->left = newNode(stoi(currVal));
62+
63+
// Push it to the queue
64+
queue.push(currNode->left);
65+
}
66+
67+
// For the right child
68+
i++;
69+
if(i >= ip.size())
70+
break;
71+
currVal = ip[i];
72+
73+
// If the right child is not null
74+
if(currVal != "N") {
75+
76+
// Create the right child for the current node
77+
currNode->right = newNode(stoi(currVal));
78+
79+
// Push it to the queue
80+
queue.push(currNode->right);
81+
}
82+
i++;
83+
}
84+
85+
return root;
86+
}
87+
88+
89+
90+
// } Driver Code Ends
91+
/* Structure for tree and linked list
92+
93+
struct Node
94+
{
95+
int data;
96+
struct Node* left;
97+
struct Node* right;
98+
99+
Node(int x){
100+
data = x;
101+
left = right = NULL;
102+
}
103+
};
104+
*/
105+
106+
// This function should return head to the DLL
107+
class Solution
108+
{
109+
public:
110+
void *bToDLLHelper(Node *root, Node * &head, Node * &prev) {
111+
112+
if(!root) return NULL;
113+
114+
bToDLLHelper(root->left, head, prev);
115+
116+
if(prev == NULL) {
117+
head = root;
118+
}
119+
else {
120+
root->left = prev;
121+
prev->right = root;
122+
}
123+
prev = root;
124+
125+
bToDLLHelper(root->right, head, prev);
126+
}
127+
128+
129+
//Function to convert binary tree to doubly linked list and return it.
130+
Node * bToDLL(Node *root) {
131+
// your code here
132+
133+
134+
Node *head = NULL;
135+
Node *prev = NULL;
136+
bToDLLHelper(root, head, prev);
137+
138+
return head;
139+
140+
}
141+
};
142+
143+
144+
145+
// { Driver Code Starts.
146+
147+
148+
/* Function to print nodes in a given doubly linked list */
149+
void printList(Node *node)
150+
{
151+
Node *prev = NULL;
152+
while (node!=NULL)
153+
{
154+
cout << node->data << " ";
155+
prev = node;
156+
node = node->right;
157+
}
158+
cout << endl;
159+
while (prev!=NULL)
160+
{
161+
cout << prev->data << " ";
162+
prev = prev->left;
163+
}
164+
cout << endl;
165+
}
166+
167+
void inorder(Node *root)
168+
{
169+
if (root != NULL)
170+
{
171+
inorder(root->left);
172+
cout << root->data;
173+
inorder(root->right);
174+
}
175+
}
176+
177+
/* Driver program to test size function*/
178+
int main()
179+
{
180+
int t;
181+
cin >> t;
182+
getchar();
183+
184+
while (t--)
185+
{
186+
string inp;
187+
getline(cin, inp);
188+
Node *root = buildTree(inp);
189+
190+
Solution ob;
191+
Node *head = ob.bToDLL(root);
192+
printList(head);
193+
194+
}
195+
return 0;
196+
}
197+
198+
// } Driver Code Ends

0 commit comments

Comments
 (0)