Last active
November 4, 2025 12:40
-
-
Save lakshyaraj2006/b0f4f2abbc3031a8119907599dee0587 to your computer and use it in GitHub Desktop.
Find the two nodes in a bst whose sum equals k, in a given range [x, y].
This file contains hidden or 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
| #include<stdio.h> | |
| #include<stdlib.h> | |
| struct TreeNode { | |
| int key; | |
| struct TreeNode* left; | |
| struct TreeNode* right; | |
| }; | |
| struct TreeNode* createNode(int key) { | |
| struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct TreeNode)); | |
| newNode->key = key; | |
| newNode->left = NULL; | |
| newNode->right = NULL; | |
| return newNode; | |
| } | |
| struct TreeNode* insert(struct TreeNode* root, int key) { | |
| if (root == NULL) { | |
| root = createNode(key); | |
| } else if (key < root->key) { | |
| root->left = insert(root->left, key); | |
| } else { | |
| root->right = insert(root->right, key); | |
| } | |
| return root; | |
| } | |
| int countNodes(struct TreeNode* root) { | |
| if (root == NULL) return 0; | |
| return 1 + countNodes(root->left) + countNodes(root->right); | |
| } | |
| void convertToArray(struct TreeNode* root, int* arr, int* index) { | |
| if (root == NULL) { | |
| return; | |
| } | |
| convertToArray(root->left, arr, index); | |
| arr[(*index)++] = root->key; | |
| convertToArray(root->right, arr, index); | |
| } | |
| int main() { | |
| struct TreeNode* root = NULL; | |
| root = insert(root, 10); | |
| root = insert(root, 8); | |
| root = insert(root, 7); | |
| root = insert(root, 9); | |
| root = insert(root, 12); | |
| root = insert(root, 13); | |
| root = insert(root, 14); | |
| root = insert(root, 15); | |
| int n = countNodes(root); | |
| int* arr = (int*)malloc(n * sizeof(int)); | |
| int index = 0; | |
| convertToArray(root, arr, &index); | |
| int left = 0, right = n-1; | |
| int sum = 17; | |
| int x = 7, y = 12; | |
| int nums[2]; | |
| int found = 0; | |
| while (left < n && arr[left] < x) { | |
| left++; | |
| } | |
| while (right >= 0 && arr[right] > y) { | |
| right--; | |
| } | |
| if (left <= right) { | |
| int l1 = left, r1 = right; | |
| while (l1 <= r1) | |
| { | |
| if (arr[l1] + arr[r1] == sum) { | |
| nums[0] = arr[l1]; | |
| nums[1] = arr[r1]; | |
| found = 1; | |
| break; | |
| } else if (arr[l1] + arr[r1] < sum) { | |
| l1++; | |
| } else if (arr[left] + arr[r1] > sum) { | |
| r1--; | |
| } | |
| } | |
| if (!found) { | |
| printf("No pairs found in range [%d, %d] whose sum equals %d\n", x, y, sum); | |
| } else { | |
| for (int j = 0; j < 2; j++) | |
| { | |
| printf("%d ", nums[j]); | |
| } | |
| } | |
| printf("\n"); | |
| } else { | |
| printf("No elements found in range [%d, %d]\n", x, y); | |
| } | |
| // Free array from memory | |
| free(arr); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment