Skip to content

Commit b4da529

Browse files
Create convert_binary_tree_to_circular_doubly_linked_list.cpp
1 parent b04aafa commit b4da529

File tree

1 file changed

+175
-0
lines changed

1 file changed

+175
-0
lines changed
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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+
25+
26+
void displayCList(Node *head)
27+
{
28+
Node *itr = head;
29+
do
30+
{
31+
cout << itr->data <<" ";
32+
itr = itr->right;
33+
} while (head!=itr);
34+
cout <<endl;
35+
itr=itr->left;
36+
do{
37+
cout<<itr->data<<" ";
38+
itr=itr->left;
39+
}while(head!=itr);
40+
cout<<itr->data<<endl;
41+
}
42+
43+
// Function to Build Tree
44+
Node* buildTree(string str)
45+
{
46+
// Corner Case
47+
if(str.length() == 0 || str[0] == 'N')
48+
return NULL;
49+
50+
// Creating vector of strings from input
51+
// string after spliting by space
52+
vector<string> ip;
53+
54+
istringstream iss(str);
55+
for(string str; iss >> str; )
56+
ip.push_back(str);
57+
58+
// Create the root of the tree
59+
Node* root = newNode(stoi(ip[0]));
60+
61+
// Push the root to the queue
62+
queue<Node*> queue;
63+
queue.push(root);
64+
65+
// Starting from the second element
66+
int i = 1;
67+
while(!queue.empty() && i < ip.size()) {
68+
69+
// Get and remove the front of the queue
70+
Node* currNode = queue.front();
71+
queue.pop();
72+
73+
// Get the current node's value from the string
74+
string currVal = ip[i];
75+
76+
// If the left child is not null
77+
if(currVal != "N") {
78+
79+
// Create the left child for the current node
80+
currNode->left = newNode(stoi(currVal));
81+
82+
// Push it to the queue
83+
queue.push(currNode->left);
84+
}
85+
86+
// For the right child
87+
i++;
88+
if(i >= ip.size())
89+
break;
90+
currVal = ip[i];
91+
92+
// If the right child is not null
93+
if(currVal != "N") {
94+
95+
// Create the right child for the current node
96+
currNode->right = newNode(stoi(currVal));
97+
98+
// Push it to the queue
99+
queue.push(currNode->right);
100+
}
101+
i++;
102+
}
103+
104+
return root;
105+
}
106+
107+
108+
// } Driver Code Ends
109+
/*Complete the function below
110+
Node is as follows:
111+
struct Node
112+
{
113+
int data;
114+
struct Node* left;
115+
struct Node* right;
116+
117+
Node(int x){
118+
data = x;
119+
left = right = NULL;
120+
}
121+
};*/
122+
123+
class Solution
124+
{
125+
public:
126+
void *bToDLLHelper(Node *root, Node * &head, Node * &prev) {
127+
128+
if(!root) return NULL;
129+
130+
bToDLLHelper(root->left, head, prev);
131+
132+
if(prev == NULL) {
133+
head = root;
134+
}
135+
else {
136+
root->left = prev;
137+
prev->right = root;
138+
}
139+
prev = root;
140+
141+
bToDLLHelper(root->right, head, prev);
142+
}
143+
144+
//Function to convert binary tree into circular doubly linked list.
145+
Node *bTreeToCList(Node *root)
146+
{
147+
//add code here.
148+
Node *head = NULL;
149+
Node *prev = NULL;
150+
bToDLLHelper(root, head, prev);
151+
head->left = prev;
152+
prev->right = head;
153+
154+
return head;
155+
}
156+
};
157+
158+
// { Driver Code Starts.
159+
int main() {
160+
int t;
161+
string tc;
162+
getline(cin, tc);
163+
t=stoi(tc);
164+
while(t--)
165+
{
166+
string s;
167+
getline(cin, s);
168+
Node* root = buildTree(s);
169+
Solution obj;
170+
Node *head = obj.bTreeToCList(root);
171+
displayCList(head);
172+
}
173+
return 0;
174+
}
175+
// } Driver Code Ends

0 commit comments

Comments
 (0)