This file contains 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
void fun(int x) { | |
// .... | |
fun(x-1); | |
// .... | |
} |
This file contains 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
int main() { | |
int non_recursive_factorial_3 = factorial3(); | |
int recursive_factorial_3 = factorial(3); | |
return 0; | |
} |
This file contains 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
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; |
This file contains 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
int factorial(int n) { | |
if (n == 1) { | |
return 1; | |
} | |
int fact_n_1 = factorial(n-1); | |
return n * fact_n_1; | |
} |
This file contains 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
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); |
This file contains 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
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]; | |
} |
This file contains 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
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); | |
} |
This file contains 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
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; | |
} |
This file contains 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
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); |
This file contains 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
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); | |
} |
OlderNewer