-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9d5513e
commit 04ecffe
Showing
11 changed files
with
723 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
12_Linked_List/SLL_Problems/Medium/725_Split_Linked_List_in_Parts.cpp
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
725. Split Linked List in Parts | ||
Given the head of a singly linked list and an integer k, split the linked list into k consecutive linked list parts. | ||
The length of each part should be as equal as possible: no two parts should have a size differing by more than one. This may lead to some parts being null. | ||
The parts should be in the order of occurrence in the input list, and parts occurring earlier should always have a size greater than or equal to parts occurring later. | ||
Return an array of the k parts. | ||
Example 1: | ||
Input: head = [1,2,3], k = 5 | ||
Output: [[1],[2],[3],[],[]] | ||
Explanation: | ||
The first element output[0] has output[0].val = 1, output[0].next = null. | ||
The last element output[4] is null, but its string representation as a ListNode is []. | ||
Example 2: | ||
Input: head = [1,2,3,4,5,6,7,8,9,10], k = 3 | ||
Output: [[1,2,3,4],[5,6,7],[8,9,10]] | ||
Explanation: | ||
The input has been split into consecutive parts with size difference at most 1, and earlier parts are a larger size than the later parts. | ||
Constraints: | ||
The number of nodes in the list is in the range [0, 1000]. | ||
0 <= Node.val <= 1000 | ||
1 <= k <= 50 | ||
*/ | ||
/** | ||
* Definition for singly-linked list. | ||
* struct ListNode { | ||
* int val; | ||
* ListNode *next; | ||
* ListNode() : val(0), next(nullptr) {} | ||
* ListNode(int x) : val(x), next(nullptr) {} | ||
* ListNode(int x, ListNode *next) : val(x), next(next) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
public: | ||
vector<ListNode*> splitListToParts(ListNode* head, int k) { | ||
vector<ListNode*>ans; | ||
ListNode* temp=head; | ||
int c=0; | ||
while(temp){ | ||
c++; | ||
temp=temp->next; | ||
} | ||
int equal=c/k,extra=c%k; | ||
ListNode* prevHead=head; | ||
while(k--){ | ||
int t=equal; | ||
if(extra>0) t++; | ||
t--; | ||
ListNode* newHead=prevHead; | ||
ListNode* temp=newHead; | ||
while(newHead && t--){ | ||
newHead=newHead->next; | ||
} | ||
if(newHead){ | ||
prevHead=newHead->next; | ||
newHead->next=NULL; | ||
} | ||
ans.push_back(temp); | ||
extra--; | ||
} | ||
return ans; | ||
} | ||
}; |
57 changes: 57 additions & 0 deletions
57
15_Tree/Problems/Medium/1367_Linked_List _in_Binary_Tree.cpp
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
1367. Linked List in Binary Tree | ||
Given a binary tree root and a linked list with head as the first node. | ||
Return True if all the elements in the linked list starting from the head correspond to some downward path connected in the binary tree otherwise return False. | ||
In this context downward path means a path that starts at some node and goes downwards. | ||
Example 1: | ||
Input: head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] | ||
Output: true | ||
Explanation: Nodes in blue form a subpath in the binary Tree. | ||
Example 2: | ||
Input: head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] | ||
Output: true | ||
Example 3: | ||
Input: head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] | ||
Output: false | ||
Explanation: There is no path in the binary tree that contains all the elements of the linked list from head. | ||
Constraints: | ||
The number of nodes in the tree will be in the range [1, 2500]. | ||
The number of nodes in the list will be in the range [1, 100]. | ||
1 <= Node.val <= 100 for each node in the linked list and binary tree. | ||
*/ | ||
/** | ||
* Definition for singly-linked list. | ||
* struct ListNode { | ||
* int val; | ||
* ListNode *next; | ||
* ListNode() : val(0), next(nullptr) {} | ||
* ListNode(int x) : val(x), next(nullptr) {} | ||
* ListNode(int x, ListNode *next) : val(x), next(next) {} | ||
* }; | ||
*/ | ||
/** | ||
* Definition for a binary tree node. | ||
* struct TreeNode { | ||
* int val; | ||
* TreeNode *left; | ||
* TreeNode *right; | ||
* TreeNode() : val(0), left(nullptr), right(nullptr) {} | ||
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | ||
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
public: | ||
bool dfs(ListNode*head,ListNode*curr, TreeNode* root){ | ||
if(!curr) return 1; | ||
if(!root) return 0; | ||
if(curr->val == root->val) curr = curr->next; | ||
else if( head->val == root->val) head = head->next; | ||
else curr = head; | ||
return dfs(head,curr,root->left) || dfs(head,curr,root->right); | ||
} | ||
bool isSubPath(ListNode*&head, TreeNode* root) { | ||
// ListNode* curr = head; | ||
return dfs(head,head,root); | ||
} | ||
}; |
72 changes: 72 additions & 0 deletions
72
15_Tree/Problems/Medium/LCA_Lowest_Common_Ancestor_of_a_Binary_Search_Tree.cpp
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
235. Lowest Common Ancestor of a Binary Search Tree | ||
Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST. | ||
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).” | ||
Example 1: | ||
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8 | ||
Output: 6 | ||
Explanation: The LCA of nodes 2 and 8 is 6. | ||
Example 2: | ||
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4 | ||
Output: 2 | ||
Explanation: The LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition. | ||
Example 3: | ||
Input: root = [2,1], p = 2, q = 1 | ||
Output: 2 | ||
Constraints: | ||
The number of nodes in the tree is in the range [2, 105]. | ||
-109 <= Node.val <= 109 | ||
All Node.val are unique. | ||
p != q | ||
p and q will exist in the BST. | ||
*/ | ||
/** | ||
* Definition for a binary tree node. | ||
* struct TreeNode { | ||
* int val; | ||
* TreeNode *left; | ||
* TreeNode *right; | ||
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | ||
* }; | ||
*/ | ||
// Optimization Approach | ||
class Solution { | ||
public: | ||
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { | ||
if(!root) return NULL; | ||
int curr= root->val; | ||
if(p->val>curr&&q->val>curr){ | ||
return lowestCommonAncestor(root->right,p,q); | ||
} | ||
if(p->val<curr&&q->val<curr){ | ||
return lowestCommonAncestor(root->left,p,q); | ||
} | ||
return root; | ||
} | ||
}; | ||
// Optimization Approach | ||
class Solution { | ||
public: | ||
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { | ||
if(root->val==p->val || root->val==q->val) return root; | ||
if(root->val<p->val && root->val<q->val) return lowestCommonAncestor(root->right,p,q); | ||
else if(root->val>p->val && root->val>q->val) return lowestCommonAncestor(root->left, p, q); | ||
return root; | ||
} | ||
}; | ||
// Approach | ||
class Solution { | ||
public: | ||
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { | ||
if(!root) return nullptr; | ||
if(root->val == p->val || root->val == q->val) return root; | ||
|
||
TreeNode* leftAns = lowestCommonAncestor(root->left,p,q); | ||
TreeNode* rightAns = lowestCommonAncestor(root->right,p,q); | ||
|
||
if(leftAns && rightAns) return root; | ||
else if(!leftAns && rightAns) return rightAns; | ||
else if(leftAns && !rightAns ) return leftAns; | ||
else return nullptr; | ||
} | ||
}; |
68 changes: 68 additions & 0 deletions
68
15_Tree/Problems/Medium/Lowest_Common_Ancestor_in_a_BST.cpp
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
Lowest Common Ancestor in a BST | ||
Given a Binary Search Tree (with all values unique) and two node values n1 and n2 (n1!=n2). Find the Lowest Common Ancestors of the two nodes in the BST. | ||
Example 1: | ||
Input: | ||
5 | ||
/ \ | ||
4 6 | ||
/ \ | ||
3 7 | ||
\ | ||
8 | ||
n1 = 7, n2 = 8 | ||
Output: 7 | ||
Example 2: | ||
Input: | ||
2 | ||
/ \ | ||
1 3 | ||
n1 = 1, n2 = 3 | ||
Output: 2 | ||
Your Task: | ||
You don't need to read input or print anything. Your task is to complete the function LCA() which takes the root Node of the BST and two integer values n1 and n2 as inputs and returns the Lowest Common Ancestor of the Nodes with values n1 and n2 in the given BST. | ||
Expected Time Complexity: O(Height of the BST). | ||
Expected Auxiliary Space: O(Height of the BST). | ||
Constraints: | ||
1 <= N <= 104 | ||
*/ | ||
//Function to find the lowest common ancestor in a BST. | ||
/* | ||
Time Complexity: O(H). where H is the height of the tree | ||
Auxiliary Space: O(1). The space complexity of the above solution is constant. | ||
*/ | ||
class Solution{ | ||
public: | ||
Node* LCA(Node *root, int n1, int n2) | ||
{ | ||
while (root != NULL) { | ||
// If both n1 and n2 are smaller than root, | ||
// then LCA lies in left | ||
if (root->data > n1 && root->data > n2) | ||
root = root->left; | ||
// If both n1 and n2 are greater than root, | ||
// then LCA lies in right | ||
else if (root->data < n1 && root->data < n2) | ||
root = root->right; | ||
else | ||
break; | ||
} | ||
return root; | ||
} | ||
}; | ||
/* | ||
Time Complexity: O(H). where H is the height of the tree. | ||
Auxiliary Space: O(H), If recursive stack space is ignored, the space complexity of the above solution is constant. | ||
*/ | ||
class Solution{ | ||
public: | ||
Node* LCA(Node *root, int n1, int n2) | ||
{ | ||
// code here | ||
if(!root) return nullptr; | ||
int curr = root->data; | ||
if(curr < n1 && curr <n2) return LCA(root->right,n1,n2); | ||
if(curr > n1 && curr > n2)return LCA(root->left,n1,n2); | ||
return root; | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
Pair Sum in BST | ||
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. | ||
Example 1: | ||
Input: | ||
2 | ||
/ \ | ||
1 3 | ||
sum = 5 | ||
Output: 1 | ||
Explanation: | ||
Nodes with value 2 and 3 sum up to 5. | ||
Example 2: | ||
Input: | ||
6 | ||
/ | ||
5 | ||
/ | ||
3 | ||
/ \ | ||
1 4 | ||
sum = 2 | ||
Output: 0 | ||
Explanation: | ||
There's no pair that sums up to 2. | ||
Your Task: | ||
You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. | ||
Expected Time Complexity: O(N). | ||
Expected Auxiliary Space: O(Height of the BST). | ||
Constraints: | ||
1 ≤ Number of Nodes ≤ 105 | ||
1 ≤ Sum ≤ 106 | ||
*/ | ||
/*Complete the function below | ||
Node is as follows | ||
struct Node { | ||
int data; | ||
Node *left; | ||
Node *right; | ||
Node(int val) { | ||
data = val; | ||
left = right = NULL; | ||
} | ||
}; | ||
*/ | ||
/* | ||
Using HashSet – O(n) time and O(n) space | ||
Time Complexity: O(n), for the visiting each node of BST | ||
Auxiliary Space: O(n), for storing nodes in hashSet | ||
*/ | ||
class Solution{ | ||
public: | ||
// root : the root Node of the given BST | ||
// target : the target sum | ||
int dfs(Node* root,unordered_set<int> &set,int target){ | ||
if(!root) return 0; | ||
if(set.count(target-root->data)) return 1; | ||
set.insert(root->data); | ||
return dfs(root->left,set,target) || dfs(root->right,set,target); | ||
|
||
} | ||
int isPairPresent(struct Node *root, int target) | ||
{ | ||
//add code here. | ||
unordered_set<int> set; | ||
return dfs(root,set,target); | ||
} | ||
}; | ||
/* | ||
USING INORDER TRAVERSAL | ||
Time Complexity: O(n), for the inorder traversal and checking the pair. | ||
Auxiliary Space: O(n), for storing the inorder traversal. | ||
*/ | ||
class Solution{ | ||
public: | ||
// root : the root Node of the given BST | ||
// target : the target sum | ||
vector<int> inorder(Node* root){ | ||
vector<int> ans; | ||
if(!root)return ans; | ||
vector<int> left = inorder(root->left) ; | ||
ans.insert(ans.end(),left.begin(),left.end()); | ||
ans.push_back(root->data); | ||
vector<int> right = inorder(root->right); | ||
ans.insert(ans.end(),right.begin(),right.end()); | ||
return ans; | ||
} | ||
int isPairPresent(struct Node *root, int target) | ||
{ | ||
//add code here. | ||
vector<int> ans = inorder(root); | ||
int i =0 , j = ans.size()-1; | ||
while(i<j){ | ||
if((ans[i]+ans[j]) == target) return 1; | ||
else if((ans[i]+ans[j]) > target) j--; | ||
else i++; | ||
} | ||
return 0; | ||
} | ||
}; |
Oops, something went wrong.