Skip to content

Instantly share code, notes, and snippets.

View bhaveshmunot1's full-sized avatar

Bhavesh Munot bhaveshmunot1

View GitHub Profile
@bhaveshmunot1
bhaveshmunot1 / DepthFirstTraversalUsingRecursion.cpp
Created June 3, 2020 09:08
Print Depth First Traversal using recursion.
void print_depth_first_traversal(GraphNode *node) {
if (is_visited(node)) {
return;
}
mark_visited(node);
for (GraphNode *neighbor : node->neighbors) {
print_depth_first_traversal(neighbor);
}
@bhaveshmunot1
bhaveshmunot1 / TailRecursionStructure.cpp
Created June 3, 2020 09:09
Tail Recursion structure.
int fun(int n) {
// ...
return fun(n-1);
}
@bhaveshmunot1
bhaveshmunot1 / Factorial_of_3_recursion_unfolded.cpp
Created June 3, 2020 09:23
Without recursion implementation to demonstrate how recursion unfolds.
int factorial_1() {
return 1;
}
int factorial_2() {
int fact_1 = factorial_1();
return 2 * fact_1;
}
int factorial_3() {
@bhaveshmunot1
bhaveshmunot1 / FactorialOf3.cpp
Created June 3, 2020 09:28
Factorial of 3 using recursion.
int factorial(int n) {
if (n == 1) {
return 1;
}
int fact_n_1 = factorial(n-1);
return n * fact_n_1;
}
@bhaveshmunot1
bhaveshmunot1 / IndirectRecursionStructure2.cpp
Created June 4, 2020 00:47
Part 2 of Indirect Recursion structure.
int fun2(int x) {
// ....
fun1(x-1);
// ....
}
@bhaveshmunot1
bhaveshmunot1 / IndirectRecursionStructure1.cpp
Last active June 4, 2020 00:47
Part 1 of Indirect Recursion structure.
int fun1(int x) {
// ....
fun2(x-1);
// ....
}
@bhaveshmunot1
bhaveshmunot1 / Leetcode_0017.cpp
Created June 4, 2020 01:00
Leetcode #17: Letter Combinations of a Phone Number
class Solution {
public:
map<char, string> numToString = {{'2', "abc"}, {'3', "def"}, {'4', "ghi"},
{'5', "jkl"}, {'6', "mno"}, {'7', "pqrs"},
{'8', "tuv"}, {'9', "wxyz"}};
vector<string> answer;
string input;
void recurse(string path, int index) {
if (!(index < input.length())) { // the entire string is processed already.
@bhaveshmunot1
bhaveshmunot1 / Leetcode_0022.cpp
Created June 4, 2020 01:02
Leetcode #22: Generate Parentheses
class Solution {
char openingParenthesis = '(';
char closingParenthesis = ')';
vector<string> answer;
void recurse(string &path, int unusedOpen, int unusedClose) {
if (unusedOpen < 0 || unusedClose < 0) { // invalid state.
return;
}
if (unusedOpen > unusedClose) { // path is not well-formed.
@bhaveshmunot1
bhaveshmunot1 / Leetcode_0039.cpp
Last active June 4, 2020 01:05
[Approach 1] Leetcode #39: Combination Sum
class Solution {
public:
vector<vector<int>> answer;
vector<int> numbers;
void recurse(vector<int> &path, int index, int target) {
if (target == 0) { // no more numbers are required.
answer.push_back(path); // add current numbers into the answer.
return;
}
@bhaveshmunot1
bhaveshmunot1 / Leetcode_0039.cpp
Created June 4, 2020 01:06
[Approach 2] Leetcode #39: Combination Sum
class Solution {
public:
vector<vector<int>> answer;
vector<int> numbers;
void recurse(vector<int> &path, int startIndex, int target) {
if (target == 0) { // no more numbers are required.
answer.push_back(path); // add current numbers into the answer.
return;
}