-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathBSTtoMaxheap.cpp
35 lines (27 loc) · 860 Bytes
/
BSTtoMaxheap.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
// BST to Max Heap
// GFG
// question link : https://practice.geeksforgeeks.org/problems/bst-to-max-heap/1
class Solution{
public:
void inorder(Node *root, vector<int> &elements) {
if(root == NULL) return ;
inorder(root->left, elements);
elements.push_back(root->data);
inorder(root->right, elements);
}
void fillTree(Node *root, vector<int> &elements, int &index) {
if(root == NULL) return ;
fillTree(root->left, elements, index);
fillTree(root->right, elements, index);
root->data = elements[index];
++index;
}
void convertToMaxHeapUtil(Node* root)
{
// Your code goes here
vector<int> elements;
inorder(root, elements);
int index = 0;
fillTree(root, elements, index);
}
};