Skip to content

Commit 39bdfb5

Browse files
authored
Update Merge Two BST's To Return BST.cpp
1 parent e3773e3 commit 39bdfb5

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed
Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,75 @@
11
class Solution {
22
public:
3+
// Helper function to perform in-order traversal and store the result in 'bst'
34
void inOrder(Node* root, vector<int> &bst){
5+
// Base case: If the current node is NULL, return
46
if(root == NULL) return;
57

8+
// Traverse the left subtree
69
inOrder(root -> left, bst);
10+
11+
// Store the current node's data in 'bst'
712
bst.push_back(root -> data);
13+
14+
// Traverse the right subtree
815
inOrder(root -> right, bst);
916
}
1017

18+
// Helper function to convert a sorted array into a balanced BST
1119
Node* inorderToBST(int start, int end, vector<int> &merged){
20+
// Base case: If the start index is greater than the end, return NULL (no subtree)
1221
if(start > end) return NULL;
1322

23+
// Calculate the middle element to maintain balance in the BST
1424
int mid = start + (end - start) / 2;
25+
26+
// Create a new node with the middle element
1527
Node* root = new Node(merged[mid]);
1628

29+
// Recursively build the left subtree using the left half of the array
1730
root -> left = inorderToBST(start, mid - 1, merged);
31+
32+
// Recursively build the right subtree using the right half of the array
1833
root -> right = inorderToBST(mid + 1, end, merged);
1934

35+
// Return the root of the constructed subtree
2036
return root;
2137
}
2238

39+
// Main function to merge two BSTs into a single balanced BST
2340
Node* merge(Node *root1, Node *root2) {
2441
vector<int> bst1, bst2;
2542

43+
// Perform in-order traversal of both trees and store the results in 'bst1' and 'bst2'
2644
inOrder(root1, bst1);
2745
inOrder(root2, bst2);
2846

47+
// Merge the two sorted arrays into a single sorted array 'merged'
2948
vector<int> merged;
3049

31-
int i = 0;
32-
int j = 0;
50+
int i = 0, j = 0;
51+
52+
// Merge the two sorted arrays while there are elements in both
3353
while(i < bst1.size() && j < bst2.size()){
54+
// Compare the current elements from both arrays and add the smaller one to 'merged'
3455
if(bst1[i] < bst2[j]){
35-
merged.push_back(bst1[i++]);
56+
merged.push_back(bst1[i++]); // Increment i after adding the element
3657
}else{
37-
merged.push_back(bst2[j++]);
58+
merged.push_back(bst2[j++]); // Increment j after adding the element
3859
}
3960
}
4061

62+
// If there are remaining elements in bst1, add them to 'merged'
4163
while(i < bst1.size()){
4264
merged.push_back(bst1[i++]);
4365
}
4466

67+
// If there are remaining elements in bst2, add them to 'merged'
4568
while(j < bst2.size()){
4669
merged.push_back(bst2[j++]);
4770
}
4871

72+
// Convert the sorted array 'merged' into a balanced BST and return the root
4973
return inorderToBST(0, merged.size()-1, merged);
5074
}
5175
};

0 commit comments

Comments
 (0)