diff --git a/Convert_Sorted_Array_To_BST.cpp b/Convert_Sorted_Array_To_BST.cpp new file mode 100644 index 0000000..40dbfbe --- /dev/null +++ b/Convert_Sorted_Array_To_BST.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + TreeNode* solve(vector &nums, int start, int end){ + //base case + if(start > end){ + return NULL; + } + + int mid = (start + end)/2; + TreeNode* temp = new TreeNode(nums[mid]); + + temp -> left = solve(nums, start, mid-1); + temp -> right = solve(nums, mid+1, end); + return temp; + } + + TreeNode* sortedArrayToBST(vector& nums) { + return solve(nums, 0, nums.size()-1); + } +}; \ No newline at end of file diff --git a/Intersection_of_Two_LL.cpp b/Intersection_of_Two_LL.cpp new file mode 100644 index 0000000..0336875 --- /dev/null +++ b/Intersection_of_Two_LL.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { + ListNode* a = headA, *b = headB; + while(a != b){ + if(a == NULL){ + a = headB; + } + else{ + a = a -> next; + } + + if(b == NULL){ + b = headA; + } + else{ + b = b -> next; + } + } + return a; + } +}; \ No newline at end of file diff --git a/N_Queen.cpp b/N_Queen.cpp new file mode 100644 index 0000000..9dec166 --- /dev/null +++ b/N_Queen.cpp @@ -0,0 +1,36 @@ +class Solution { +public: + void solve(int col, vector &board, vector> &ans, vector &leftRow, vector &upperDiagonal, vector &lowerDiagonal, int n){ + //base case + if(col == n){ + ans.push_back(board); + return; + } + + for(int row=0; row> solveNQueens(int n) { + vector> ans; + vector board(n); + string s(n, '.'); + for(int i=0; i leftRow(n, 0), upperDiagonal(2 * n - 1, 0), lowerDiagonal(2 * n - 1, 0); + solve(0, board, ans, leftRow, upperDiagonal, lowerDiagonal, n); + return ans; + } +}; \ No newline at end of file