Skip to content

Commit 9106263

Browse files
authored
Create main.cpp
1 parent 811fbea commit 9106263

File tree

1 file changed

+28
-0
lines changed
  • 19 - Heap Data Structure Problems/10 - BST to Max Heap

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution{
2+
public:
3+
void inOrder(Node* root, vector<int> &nodes){
4+
if(root == NULL) return;
5+
6+
inOrder(root -> left, nodes);
7+
nodes.push_back(root -> data);
8+
inOrder(root -> right, nodes);
9+
}
10+
11+
void inOrderToHeap(Node* root, vector<int> &nodes, int & index){
12+
if(root == NULL) return;
13+
14+
inOrderToHeap(root -> left, nodes, index);
15+
inOrderToHeap(root -> right, nodes, index);
16+
17+
root -> data = nodes[index++];
18+
}
19+
20+
void convertToMaxHeapUtil(Node* root)
21+
{
22+
vector<int> nodes;
23+
inOrder(root, nodes);
24+
25+
int index = 0;
26+
inOrderToHeap(root, nodes, index);
27+
}
28+
};

0 commit comments

Comments
 (0)