Created
March 30, 2026 23:59
-
-
Save raiyansarker/2eeaf1a40b5179a291c6471dc5709b22 to your computer and use it in GitHub Desktop.
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> | |
| typedef struct Node { | |
| int data; | |
| struct Node *left, *right; | |
| } Node; | |
| Node* createNode(int data) { | |
| Node* n = (Node*)calloc(1, sizeof(Node)); | |
| n->data = data; | |
| return n; | |
| } | |
| Node *insert(Node *root, int data) { | |
| if (!root) return createNode(data); | |
| if (data < root->data) root->left = insert(root->left, data); | |
| else root->right = insert(root->right, data); | |
| return root; | |
| } | |
| int search(Node *root, int s) { | |
| if (!root) return 0; | |
| if (root->data == s) return 1; | |
| if (s < root->data) return search(root->left, s); | |
| else return search(root->right, s); | |
| } | |
| int main() { | |
| int arr[] = {10, 16, 4, 25, 54, 12, 8, 3, 20, 32}; | |
| int n = sizeof(arr) / sizeof(arr[0]); | |
| Node *root = NULL; | |
| for (int i = 0; i < n; i++) root = insert(root, arr[i]); | |
| int s; scanf("%d", &s); | |
| if (search(root, s)) { | |
| printf("HIT\n"); | |
| } else { | |
| printf("MISS\n"); | |
| } | |
| } |
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> | |
| typedef struct Node { | |
| int data; | |
| struct Node *left, *right; | |
| } Node; | |
| Node* createNode(int data) { | |
| Node* n = (Node*)calloc(1, sizeof(Node)); | |
| n->data = data; | |
| return n; | |
| } | |
| int sum(Node *root) { | |
| if (!root) return 0; | |
| int s = 0; | |
| if (root->left) { | |
| if (!root->left->left && !root->left->right) s += root->left->data; | |
| else s += sum(root->left); | |
| } | |
| s += sum(root->right); | |
| return s; | |
| } | |
| int main() { | |
| Node *root = createNode(14); | |
| root->left = createNode(2); | |
| root->left->left = createNode(1); | |
| root->left->right = createNode(3); | |
| root->right = createNode(11); | |
| root->right->left = createNode(10); | |
| root->right->left->left = createNode(7); | |
| root->right->right = createNode(30); | |
| root->right->right->left = createNode(40); | |
| printf("Sum of left leaves: %d\n", sum(root)); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment