From ae1b41dc493049d88ed3409e74705b740424395f Mon Sep 17 00:00:00 2001 From: Ashay Walke Date: Thu, 4 Oct 2018 14:15:23 +0530 Subject: [PATCH 1/6] Create Postorder_Traversal.cpp --- Tree Data Structure/Postorder_Traversal.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Tree Data Structure/Postorder_Traversal.cpp diff --git a/Tree Data Structure/Postorder_Traversal.cpp b/Tree Data Structure/Postorder_Traversal.cpp new file mode 100644 index 0000000..ac6e0f6 --- /dev/null +++ b/Tree Data Structure/Postorder_Traversal.cpp @@ -0,0 +1,21 @@ +/*Given a binary tree, return the postorder traversal of its nodes’ values.*/ + + +vector Solution::postorderTraversal(TreeNode* A) { + vector Answer; + stack stac; + if(!A)return Answer; + stac.push(A); + + while(!stac.empty()){ + struct TreeNode * nod = stac.top(); + Answer.push_back(nod -> val); + stac.pop(); + if(nod -> left) stac.push(nod->left); + if(nod -> right) stac.push(nod->right); + + } + reverse(Answer.begin(),Answer.end()); + return Answer; + +} From 9178b9becafd011c1d5e31bd61ad572e194a2394 Mon Sep 17 00:00:00 2001 From: Ashay Walke Date: Thu, 4 Oct 2018 14:16:23 +0530 Subject: [PATCH 2/6] Preorder Traversal --- Tree Data Structure/Preorder_Traversal.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Tree Data Structure/Preorder_Traversal.cpp diff --git a/Tree Data Structure/Preorder_Traversal.cpp b/Tree Data Structure/Preorder_Traversal.cpp new file mode 100644 index 0000000..d0186f0 --- /dev/null +++ b/Tree Data Structure/Preorder_Traversal.cpp @@ -0,0 +1,22 @@ +/*Given a binary tree, return the preorder traversal of its nodes’ values.*/ + +vector Solution::preorderTraversal(TreeNode* A){ + vector Answer; + stack stac; + if(!A) return Answer; + + stac.push(A); + while(stac.empty()==false){ + struct TreeNode* TreeNod = stac.top(); + Answer.push_back(TreeNod -> val); + stac.pop(); + + if(TreeNod -> right) + stac.push(TreeNod -> right); + if(TreeNod -> left) + stac.push(TreeNod -> left); + } + return Answer; + + +} From ae213378688029e3eba806f67e6acbbd02f302e0 Mon Sep 17 00:00:00 2001 From: Ashay Walke Date: Thu, 4 Oct 2018 14:17:55 +0530 Subject: [PATCH 3/6] Inorder Traversal --- Tree Data Structure/Inorder_Traversal.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Tree Data Structure/Inorder_Traversal.cpp diff --git a/Tree Data Structure/Inorder_Traversal.cpp b/Tree Data Structure/Inorder_Traversal.cpp new file mode 100644 index 0000000..8b08608 --- /dev/null +++ b/Tree Data Structure/Inorder_Traversal.cpp @@ -0,0 +1,22 @@ +/*Given a binary tree, return the inorder traversal of its nodes’ values*/ + +vector Solution::inorderTraversal(TreeNode* A) { + vector Answer; + stack s; + if(!A) return Answer; + + while(A!=NULL || !s.empty()){ + + while(A!=NULL){ + s.push(A); + A = A->left; + } + + A = s.top(); + s.pop(); + Answer.push_back(A->val); + + A = A->right; + } + return Answer; +} From 6966b0f58d158785db5e8e4b1b2eabbe66498887 Mon Sep 17 00:00:00 2001 From: Ashay Walke Date: Thu, 4 Oct 2018 16:28:06 +0530 Subject: [PATCH 4/6] Added 2Sum problem --- Hashing/2Sum.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Hashing/2Sum.cpp diff --git a/Hashing/2Sum.cpp b/Hashing/2Sum.cpp new file mode 100644 index 0000000..8f8f02b --- /dev/null +++ b/Hashing/2Sum.cpp @@ -0,0 +1,21 @@ +/*Given an array of integers, find two numbers such that they add up to a specific target number. + +The function twoSum should return indices of the two numbers such that they add up to the target, where index1 < index2. Please note that your returned answers (both index1 and index2 ) are not zero-based. +Put both these numbers in order in an array and return the array from your function ( Looking at the function signature will make things clearer ). Note that, if no pair exists, return empty list. + +If multiple solutions exist, output the one where index2 is minimum. If there are multiple solutions with the minimum index2, choose the one with minimum index1 out of them.*/ + +vector Solution::twoSum(const vector &A, int B) { + map Hash; + vector ans; + int n = A.size(); + for(int i=0;i Date: Thu, 4 Oct 2018 16:28:56 +0530 Subject: [PATCH 5/6] Added 4Sum --- Hashing/4Sum.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Hashing/4Sum.cpp diff --git a/Hashing/4Sum.cpp b/Hashing/4Sum.cpp new file mode 100644 index 0000000..30f9344 --- /dev/null +++ b/Hashing/4Sum.cpp @@ -0,0 +1,51 @@ +/*Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. + + Note: +Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d) +The solution set must not contain duplicate quadruplets. +Example : +Given array S = {1 0 -1 0 -2 2}, and target = 0 +A solution set is: + + (-2, -1, 1, 2) + (-2, 0, 0, 2) + (-1, 0, 0, 1) +Also make sure that the solution set is lexicographically sorted. +Solution[i] < Solution[j] iff Solution[i][0] < Solution[j][0] OR (Solution[i][0] == Solution[j][0] AND ... Solution[i][k] < Solution[j][k])*/ + +vector > Solution::fourSum(vector &A, int B) { + int n = A.size(); + sort(A.begin(),A.end()); + int i,j; + unordered_map> hash; + vector> Ans; + vectortemp; + + + for(i=0;ip = hash[B-sum]; + if(p.first != i && p.first != j && p.second != i && p.second != j){ + temp.push_back(A[i]); + temp.push_back(A[j]); + temp.push_back(A[p.first]); + temp.push_back(A[p.second]); + sort(temp.begin(),temp.end()); + Ans.push_back(temp); + temp.clear(); + } + } + } + } + sort(Ans.begin(), Ans.end()); + Ans.erase(unique(Ans.begin(), Ans.end()), Ans.end()); + return Ans; +} From cf1f78627889d15781672202331a2419c7911853 Mon Sep 17 00:00:00 2001 From: Ashay Walke Date: Thu, 4 Oct 2018 16:30:22 +0530 Subject: [PATCH 6/6] Added Fraction --- Hashing/fraction.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Hashing/fraction.cpp diff --git a/Hashing/fraction.cpp b/Hashing/fraction.cpp new file mode 100644 index 0000000..665d943 --- /dev/null +++ b/Hashing/fraction.cpp @@ -0,0 +1,48 @@ +/*Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. + +If the fractional part is repeating, enclose the repeating part in parentheses. + +Example : + +Given numerator = 1, denominator = 2, return "0.5" +Given numerator = 2, denominator = 1, return "2" +Given numerator = 2, denominator = 3, return "0.(6)"*/ + + +string Solution::fractionToDecimal(int A, int B) { + int64_t n = A, d = B; + + if(n==0) return "0"; + + string result = ""; + + if(n<0 ^ d<0) result += '-'; + + n = abs(n); + d = abs(d); + + result += to_string((n/d)); + + if(n%d == 0) return result; + + result += '.'; + + unordered_map Hash; + + for(int64_t r=n%d; r; r %= d){ + + if(Hash.find(r)!=Hash.end()){ + result.insert(Hash[r],1,'('); + result += ')'; + break; + } + + Hash[r] = result.size(); + + r *= 10; + + result += (char)('0'+(r/d)); + + } + return result; +}