-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
19 - Heap Data Structure Problems/10 - BST to Max Heap/main.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class Solution{ | ||
public: | ||
void inOrder(Node* root, vector<int> &nodes){ | ||
if(root == NULL) return; | ||
|
||
inOrder(root -> left, nodes); | ||
nodes.push_back(root -> data); | ||
inOrder(root -> right, nodes); | ||
} | ||
|
||
void inOrderToHeap(Node* root, vector<int> &nodes, int & index){ | ||
if(root == NULL) return; | ||
|
||
inOrderToHeap(root -> left, nodes, index); | ||
inOrderToHeap(root -> right, nodes, index); | ||
|
||
root -> data = nodes[index++]; | ||
} | ||
|
||
void convertToMaxHeapUtil(Node* root) | ||
{ | ||
vector<int> nodes; | ||
inOrder(root, nodes); | ||
|
||
int index = 0; | ||
inOrderToHeap(root, nodes, index); | ||
} | ||
}; |