-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarytreetodll.cpp
More file actions
81 lines (67 loc) · 1.52 KB
/
binarytreetodll.cpp
File metadata and controls
81 lines (67 loc) · 1.52 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <bits/stdc++.h>
using namespace std;
class BinaryTree
{
public:
int value;
BinaryTree*left;
BinaryTree*right;
BinaryTree(int value){
this->value = value;
left = nullptr;
right = nullptr;
}
};
void solve(BinaryTree*root,BinaryTree*&head,BinaryTree*&prev,int &flag)
{
if(root == nullptr)
{
return 0;
}
solve(root->left,head,prev,flag);
if(flag == 0)
{
flag = 1;
head = root;
prev = root;
}
else
{
prev->right = root;
prev->right->left = prev;
prev = prev->right;
}
solve(root->right,head,prev,flag);
}
BinaryTree*flattenBinaryTree(BinaryTree*root)
{
BinaryTree*head = NULL;
BinaryTree*prev = NULL;
int flag = 0;
solve(root,head,prev,flag);
return head;
}
void display(Node*root)
{
Node*temp = head;
while(temp!=NULL)
{
cout<<temp->val;
temp = temp->right;
}
}
int main() {
BinaryTree*root = new BinaryTree(1);
root->left = new BinaryTree(2);
root->right = new BinaryTree(3);
root->left->left = new BinaryTree(4);
root->left->right = new BinaryTree(5);
root->right->left = new BinaryTree(6);
root->right->right = new BinaryTree(7);
// root->left->right->right = new BinaryTree(8);
// root->right->right = new BinaryTree(7);
// root->left->left->left = new BinaryTree(8);
// root->left->left->right = new BinaryTree(9);
root = flattenBinaryTree(root);
display(root);
}