diff --git a/Linked List/merge_two_sorted_lists/MergeTwoSortedLists.cpp b/Linked List/merge_two_sorted_lists/MergeTwoSortedLists.cpp new file mode 100644 index 0000000..932ce0f --- /dev/null +++ b/Linked List/merge_two_sorted_lists/MergeTwoSortedLists.cpp @@ -0,0 +1,134 @@ +#include +using namespace std; + +// Merge Two Sorted Lists + +// You are given the heads of two sorted linked lists list1 and list2. +// Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists. +// Return the head of the merged linked list. + +// Example 1: +// Input: list1 = [1,2,4], list2 = [1,3,4] +// Output: [1,1,2,3,4,4] + +// Example 2: +// Input: list1 = [], list2 = [] +// Output: [] + +// Example 3: +// Input: list1 = [], list2 = [0] +// Output: [0] + +// Definition for singly-linked list. +struct ListNode { + int val; + ListNode* next; + + ListNode(int val) : val(val), next(nullptr) {} +}; + +class Solution { +private: + /** + * Helper function to merge two sorted linked lists. + * @param first The head of the first linked list. + * @param second The head of the second linked list. + * @return The head of the merged linked list. + */ + ListNode* solve(ListNode* first, ListNode* second) { + if (first->next == nullptr) { + first->next = second; + return first; + } + + ListNode* curr1 = first; + ListNode* next1 = first->next; + ListNode* curr2 = second; + ListNode* next2 = second->next; + + while (next1 != nullptr && curr2 != nullptr) { + if (curr2->val >= curr1->val && curr2->val <= next1->val) { + curr1->next = curr2; + next2 = curr2->next; + curr2->next = next1; + curr1 = curr2; + curr2 = next2; + } else { + curr1 = next1; + next1 = next1->next; + + if (next1 == nullptr) { + curr1->next = curr2; + return first; + } + } + } + + return first; + } + +public: + ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) { + if (list1 == nullptr) { + return list2; + } + if (list2 == nullptr) { + return list1; + } + + if (list1->val < list2->val) { + return solve(list1, list2); + } else { + return solve(list2, list1); + } + } +}; + +int main() { + Solution solution; + + // Example 1: + ListNode* list1 = new ListNode(1); + list1->next = new ListNode(2); + list1->next->next = new ListNode(4); + + ListNode* list2 = new ListNode(1); + list2->next = new ListNode(3); + list2->next->next = new ListNode(4); + + ListNode* result1 = solution.mergeTwoLists(list1, list2); + ListNode* curr1 = result1; + while (curr1 != nullptr) { + std::cout << curr1->val << " "; + curr1 = curr1->next; + } + std::cout << std::endl; + + // Example 2: + ListNode* list3 = nullptr; + + ListNode* list4 = nullptr; + + ListNode* result2 = solution.mergeTwoLists(list3, list4); + ListNode* curr2 = result2; + while (curr2 != nullptr) { + std::cout << curr2->val << " "; + curr2 = curr2->next; + } + std::cout << std::endl; + + // Example 3: + ListNode* list5 = nullptr; + + ListNode* list6 = new ListNode(0); + + ListNode* result3 = solution.mergeTwoLists(list5, list6); + ListNode* curr3 = result3; + while (curr3 != nullptr) { + std::cout << curr3->val << " "; + curr3 = curr3->next; + } + std::cout << std::endl; + + return 0; +} \ No newline at end of file diff --git a/Tree/Same_tree/SameTree.cpp b/Tree/Same_tree/SameTree.cpp new file mode 100644 index 0000000..dbad52a --- /dev/null +++ b/Tree/Same_tree/SameTree.cpp @@ -0,0 +1,84 @@ +#include +using namespace std; + +// Same Tree + +// Given the roots of two binary trees p and q, check if they are the same or not. +// Two binary trees are considered the same if they are structurally identical, and the nodes have the same value. + +// Example 1: +// Input: p = [1,2,3], q = [1,2,3] +// Output: true + +// Example 2: +// Input: p = [1,2], q = [1,null,2] +// Output: false + +// Definition for a binary tree node. +class TreeNode { +public: + int val; + TreeNode* left; + TreeNode* right; + + TreeNode(int value) : val(value), left(nullptr), right(nullptr) {} +}; + +class Solution { +public: + bool isSameTree(TreeNode* p, TreeNode* q) { + // If both trees are empty, they are identical. + if (p == nullptr && q == nullptr) { + return true; + } + + // If one tree is empty and the other is not, they are not identical. + if ((p != nullptr && q == nullptr) || (p == nullptr && q != nullptr)) { + return false; + } + + // Recursively check if left and right subtrees are identical, as well as the current node value. + bool left = isSameTree(p->left, q->left); + bool right = isSameTree(p->right, q->right); + bool value = (p->val == q->val); + + // Both subtrees and the current node value must be identical for the trees to be identical. + if (left && right && value) { + return true; + } + + return false; + } +}; + +int main() { + // Create two binary trees for demonstration purposes + TreeNode* tree1 = new TreeNode(1); + tree1->left = new TreeNode(2); + tree1->right = new TreeNode(3); + + TreeNode* tree2 = new TreeNode(1); + tree2->left = new TreeNode(2); + tree2->right = new TreeNode(3); + + Solution solution; + bool isIdentical = solution.isSameTree(tree1, tree2); + + if (isIdentical) { + std::cout << "The two binary trees are identical." << std::endl; + } else { + std::cout << "The two binary trees are not identical." << std::endl; + } + + // Clean up the memory allocated for the binary trees + // (You may want to use a proper tree traversal for more complex trees) + delete tree1->left; + delete tree1->right; + delete tree1; + + delete tree2->left; + delete tree2->right; + delete tree2; + + return 0; +} \ No newline at end of file diff --git a/Tree/maximum_depth_of_binary_tree/MaximumDepthofBinaryTree.cpp b/Tree/maximum_depth_of_binary_tree/MaximumDepthofBinaryTree.cpp new file mode 100644 index 0000000..cdabd5a --- /dev/null +++ b/Tree/maximum_depth_of_binary_tree/MaximumDepthofBinaryTree.cpp @@ -0,0 +1,66 @@ +#include +using namespace std; + +// Maximum Depth of Binary Tree + +// Given the root of a binary tree, find its maximum depth. +// A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. + +// Example 1: +// Input: root = [3,9,20,null,null,15,7] +// Output: 3 + +// Example 2: +// Input: root = [1,null,2] +// Output: 2 + +// Definition for a binary tree node. +class TreeNode { +public: + int val; + TreeNode* left; + TreeNode* right; + + TreeNode(int value) : val(value), left(nullptr), right(nullptr) {} +}; + +class Solution { +public: + int maxDepth(TreeNode* root) { + // If the tree is empty, the maximum depth is 0. + if (root == nullptr) { + return 0; + } + + // Recursively calculate the maximum depth of the left and right subtrees. + int leftHeight = maxDepth(root->left); + int rightHeight = maxDepth(root->right); + + // The maximum depth of the current node's subtree is the maximum of left and right subtrees, + // plus one to account for the current node. + return std::max(leftHeight, rightHeight) + 1; + } +}; + +int main() { + // Create a binary tree for demonstration purposes + TreeNode* root = new TreeNode(3); + root->left = new TreeNode(9); + root->right = new TreeNode(20); + root->right->left = new TreeNode(15); + root->right->right = new TreeNode(7); + + Solution solution; + int depth = solution.maxDepth(root); + + std::cout << "Maximum depth of the binary tree: " << depth << std::endl; + + // Clean up the memory allocated for the binary tree + // (You may want to use a proper tree traversal for more complex trees) + delete root->right->left; + delete root->right->right; + delete root->left; + delete root; + + return 0; +} \ No newline at end of file