-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary Tree to CDLL.cpp
51 lines (49 loc) · 961 Bytes
/
Binary Tree to CDLL.cpp
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
/*Complete the function below
Node is as follows:
struct Node
{
int data;
struct Node* left;
struct Node* right;
Node(int x){
data = x;
left = right = NULL;
}
};*/
class Solution
{
public:
//Function to convert binary tree into circular doubly linked list.
public:
void inorder(vector<int>& v, Node* root)
{
if(root==NULL)
return;
inorder(v, root->left);
v.push_back(root->data);
inorder(v, root->right);
}
//Function to convert binary tree into circular doubly linked list.
Node *bTreeToCList(Node *root)
{
//add code here.
vector<int>v;
inorder(v, root);
if(v.empty())
return NULL;
Node* r=new Node();
r->data=v[0];
Node* x=r;
for(int i=1;i<v.size();i++)
{
Node* t=new Node();
t->data=v[i];
x->right=t;
t->left=x;
x=t;
}
x->right=r;
r->left=x;
return r;
}
};