Skip to content

Instantly share code, notes, and snippets.

View bhaveshmunot1's full-sized avatar

Bhavesh Munot bhaveshmunot1

View GitHub Profile
@bhaveshmunot1
bhaveshmunot1 / recursion_structure.cpp
Created June 3, 2020 08:45
Simple Recursion Structure
void fun(int x) {
// ....
fun(x-1);
// ....
}
@bhaveshmunot1
bhaveshmunot1 / factorial_variations.cpp
Created June 3, 2020 08:53
Two variations of finding factorial of 3.
int main() {
int non_recursive_factorial_3 = factorial3();
int recursive_factorial_3 = factorial(3);
return 0;
}
@bhaveshmunot1
bhaveshmunot1 / nth_fibonacci_with_recursion.cpp
Created June 3, 2020 08:56
Find N-th Fibonacci number using recursion.
int fibonacci(int n) {
if (n == 1) {
return 0;
}
if (n == 2) {
return 1;
}
int n_minus_1_fibonacci = fibonacci(n-1);
int n_minus_2_fibonacci = fibonacci(n-2);
return n_minus_1_fibonacci + n_minus_2_fibonacci;
int factorial(int n) {
if (n == 1) {
return 1;
}
int fact_n_1 = factorial(n-1);
return n * fact_n_1;
}
@bhaveshmunot1
bhaveshmunot1 / reverse_string_with_recursion.cpp
Created June 3, 2020 08:59
Reverse a string using recursion.
string reverse(string s) {
int length = s.size();
if (length <= 1) {
return s;
}
string middle_part = string(s.begin()+1, s.end()-1);
string reversed_middle_part = reverse(middle_part);
string first_char = string(s.begin(), s.begin()+1);
@bhaveshmunot1
bhaveshmunot1 / sum_of_array_using _recursion.cpp
Created June 3, 2020 09:01
Find sum of numbers in an array using recursion.
int sum_of_array(vector<int> nums, int start_index, int end_index) {
if (start_index == end_index) {
return nums[start_index];
}
int sum = sum_of_array(nums, start_index+1, end_index);
return sum + nums[start_index];
}
@bhaveshmunot1
bhaveshmunot1 / maxInArrayUsingRecursion.cpp
Created June 3, 2020 09:03
Find maximum number in an array using recursion.
int max_in_array(vector<int> nums, int start_index, int end_index) {
if (start_index == end_index) {
return nums[start_index];
}
int maximum = max_in_array(nums, start_index+1, end_index);
return max(nums[start_index], maximum);
}
@bhaveshmunot1
bhaveshmunot1 / binarySearchUsingRecursion.cpp
Created June 3, 2020 09:04
Implement Binary Search using recursion.
bool binary_search(vector<int> sorted_nums, int low, int high, int K) {
if (low > high) {
return false;
}
int mid = (low+high)/2;
if (sorted_nums[mid] == K) {
return true;
}
@bhaveshmunot1
bhaveshmunot1 / MergeSortUsingRecursion.cpp
Created June 3, 2020 09:05
Implement Merge Sort using recursion.
vector<int> merge_sort(vector<int> nums) {
int length = nums.size();
if (length <= 1) {
return numbers;
}
int half = length/2;
vector<int> left_half = vector<int>(nums.begin(), nums.begin()+half);
vector<int> right_half = vector<int>(nums.begin()+half, nums.end());
vector<int> sorted_left = merge_sort(left_half);
@bhaveshmunot1
bhaveshmunot1 / PostOrderTraversalUsingRecursion.cpp
Created June 3, 2020 09:06
Print Post Order Traversal of a binary tree using recursion.
void print_post_order_traversal(TreeNode *root) {
if (root == nullptr) {
return;
}
print_post_order_traversal(root->left);
print_post_order_traversal(root->right);
print(root->value);
}