-
Notifications
You must be signed in to change notification settings - Fork 43
/
FlatenABinaryTree.cpp
104 lines (86 loc) · 1.82 KB
/
FlatenABinaryTree.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
//Flaten a Binary Tree
//left of each node will be null
#include<bits/stdc++.h>
using namespace std;
struct Node{
int data;
Node* left;
Node* right;
Node(int val){
data = val;
left = NULL;
right = NULL;
}
};
void printTree(Node* node)
{
if(node == NULL)
return;
cout<<node->data<<" ";
printTree(node->left);
printTree(node->right);
}
void printInOrder(Node* root)
{
if(root==NULL)
return;
printInOrder(root->left);
cout<<root->data<<" ";
printInOrder(root->right);
}
int searchIn(int in[],int start,int end,int curr)
{
for(int i=start;i<=end;i++)
{
if(in[i]==curr)
return i;
}
return -1;
}
Node* treePreIn(int pre[], int in[], int start, int end)
{
if(start>end)
return NULL;
static int idx = 0;
int curr = pre[idx];
idx++;
Node* node = new Node(curr);
if(start == end)
return node;
int pos = searchIn(in,start,end,curr);
node->left = treePreIn(pre,in,start,pos-1);
node->right = treePreIn(pre,in,pos+1,end);
return node;
}
void treeFlatten(Node* root)
{
if(root == NULL || (root->left == NULL && root->right == NULL))
return;
if(root->left != NULL)
{
treeFlatten(root->left);
Node* temp = root->right;
root->right = root->left;
root->left = NULL;
Node* t = root->right;
while(t->right!=NULL)
t = t->right;
t->right = temp;
}
treeFlatten(root->right);
}
// 4
// 9 5
// 1 3 6
int main ()
{
int preOrder[] = {4,9,1,3,5,6};
int inOrder[] = {1,9,3,4,5,6};
Node* root = treePreIn(preOrder,inOrder,0,5);
// cout<<root->right->right->data<<endl;
printTree(root);
cout<<endl;
treeFlatten(root);
// cout<<root->data;
printInOrder(root);
}