-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathBinaryTreeToLinkedList.cpp
122 lines (101 loc) · 2.52 KB
/
BinaryTreeToLinkedList.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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <bits/stdc++.h>
using namespace std;
class Node{
public:
Node* left;
Node* right;
int data;
Node(int x){
data = x;
left = NULL;
right = NULL;
}
};
class Solution{
public:
Node* insert(Node* root, int data) {
if(root == NULL) {
return new Node(data);
} else {
Node* cur;
if(data <= root->data) {
cur = insert(root->left, data);
root->left = cur;
} else {
cur = insert(root->right, data);
root->right = cur;
}
return root;
}
}
Node* flatten(Node* A) {
if(!A)return NULL;
// Take the left and right subtree
Node* tempr = A->right;
Node* templ = A->left;
// Recursively Flatten them
flatten(templ);
flatten(tempr);
// If left subtree exists, place it as the right subtree and make the right subtree
// as the child of this new right subtree
if(templ){
A->right = templ;
Node* x = templ;
// Go to the end of the right subtree (which was earlier left subtree)
while(x->right){
x = x->right;
}
// Place the earlier right subtree as the right subtree of the current right subtree
x->right = tempr;
// Make the new left subtree as NULL
templ->left = NULL;
}
// Nullify the left subtree.
A->left = NULL;
// Return the tree.
return A;
}
void inOrder(Node* tree){
if(!tree) return;
inOrder(tree->left);
cout << tree->data << " " ;
inOrder(tree->right);
}
};
// Initial Tree:
// 4
// / \
// 2 7
// / \ /
// 1 3 6
// Final Tree:
// 4
// / \
// N 2
// / \
// N 1
// / \
// N 3
// / \
// N 7
// / \
// N 6
// N - NULL
int main() {
Solution myTree;
Node* root = NULL;
root = myTree.insert(root,4);
root = myTree.insert(root,2);
root = myTree.insert(root,3);
root = myTree.insert(root,1);
root = myTree.insert(root,7);
root = myTree.insert(root,6);
cout << "InOrder Traversal is of the tree is as follows: " << endl;
myTree.inOrder(root);
cout << endl;
myTree.flatten(root);
cout << "\n\nInOrder Traversal is of the flattened tree is as follows: " << endl;
myTree.inOrder(root);
cout << endl;
return 0;
}