|
1 | 1 | class Solution { |
2 | 2 | public: |
| 3 | + // Helper function to perform in-order traversal and store the result in 'bst' |
3 | 4 | void inOrder(Node* root, vector<int> &bst){ |
| 5 | + // Base case: If the current node is NULL, return |
4 | 6 | if(root == NULL) return; |
5 | 7 |
|
| 8 | + // Traverse the left subtree |
6 | 9 | inOrder(root -> left, bst); |
| 10 | + |
| 11 | + // Store the current node's data in 'bst' |
7 | 12 | bst.push_back(root -> data); |
| 13 | + |
| 14 | + // Traverse the right subtree |
8 | 15 | inOrder(root -> right, bst); |
9 | 16 | } |
10 | 17 |
|
| 18 | + // Helper function to convert a sorted array into a balanced BST |
11 | 19 | 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) |
12 | 21 | if(start > end) return NULL; |
13 | 22 |
|
| 23 | + // Calculate the middle element to maintain balance in the BST |
14 | 24 | int mid = start + (end - start) / 2; |
| 25 | + |
| 26 | + // Create a new node with the middle element |
15 | 27 | Node* root = new Node(merged[mid]); |
16 | 28 |
|
| 29 | + // Recursively build the left subtree using the left half of the array |
17 | 30 | root -> left = inorderToBST(start, mid - 1, merged); |
| 31 | + |
| 32 | + // Recursively build the right subtree using the right half of the array |
18 | 33 | root -> right = inorderToBST(mid + 1, end, merged); |
19 | 34 |
|
| 35 | + // Return the root of the constructed subtree |
20 | 36 | return root; |
21 | 37 | } |
22 | 38 |
|
| 39 | + // Main function to merge two BSTs into a single balanced BST |
23 | 40 | Node* merge(Node *root1, Node *root2) { |
24 | 41 | vector<int> bst1, bst2; |
25 | 42 |
|
| 43 | + // Perform in-order traversal of both trees and store the results in 'bst1' and 'bst2' |
26 | 44 | inOrder(root1, bst1); |
27 | 45 | inOrder(root2, bst2); |
28 | 46 |
|
| 47 | + // Merge the two sorted arrays into a single sorted array 'merged' |
29 | 48 | vector<int> merged; |
30 | 49 |
|
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 |
33 | 53 | while(i < bst1.size() && j < bst2.size()){ |
| 54 | + // Compare the current elements from both arrays and add the smaller one to 'merged' |
34 | 55 | if(bst1[i] < bst2[j]){ |
35 | | - merged.push_back(bst1[i++]); |
| 56 | + merged.push_back(bst1[i++]); // Increment i after adding the element |
36 | 57 | }else{ |
37 | | - merged.push_back(bst2[j++]); |
| 58 | + merged.push_back(bst2[j++]); // Increment j after adding the element |
38 | 59 | } |
39 | 60 | } |
40 | 61 |
|
| 62 | + // If there are remaining elements in bst1, add them to 'merged' |
41 | 63 | while(i < bst1.size()){ |
42 | 64 | merged.push_back(bst1[i++]); |
43 | 65 | } |
44 | 66 |
|
| 67 | + // If there are remaining elements in bst2, add them to 'merged' |
45 | 68 | while(j < bst2.size()){ |
46 | 69 | merged.push_back(bst2[j++]); |
47 | 70 | } |
48 | 71 |
|
| 72 | + // Convert the sorted array 'merged' into a balanced BST and return the root |
49 | 73 | return inorderToBST(0, merged.size()-1, merged); |
50 | 74 | } |
51 | 75 | }; |
0 commit comments