-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
9 changed files
with
219 additions
and
7 deletions.
There are no files selected for viewing
File renamed without changes.
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,32 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
struct Node | ||
{ | ||
int data; | ||
Node *left; | ||
Node *right; | ||
}; | ||
|
||
bool findPairUtil(Node *root, int target, set<int> &st) | ||
{ | ||
if (root == nullptr) | ||
return false; | ||
if (findPairUtil(root->left, target, st)) | ||
{ | ||
return true; | ||
} | ||
if (st.find(target - root->data) != st.end()) | ||
{ | ||
cout << root->data << " " << target - root->data << endl; | ||
return true; | ||
} | ||
else | ||
{ | ||
st.insert(root->data); | ||
} | ||
if (findPairUtil(root->right, target, st)) | ||
{ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
struct Node | ||
{ | ||
int data; | ||
Node *left; | ||
Node *right; | ||
}; | ||
|
||
pair<int, int> floorCeil(Node *root, int key) | ||
{ | ||
if (root == nullptr) | ||
return {}; | ||
else | ||
{ | ||
pair<int, int> p; | ||
if (root->data == key) | ||
{ | ||
p.first = root->data; | ||
p.second = root->data; | ||
} | ||
else if (root->data < key) | ||
{ | ||
p.first = root->data; | ||
root = root->right; | ||
} | ||
else | ||
{ | ||
p.second = root->data; | ||
root = root->left; | ||
} | ||
return p; | ||
} | ||
} |
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,51 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
struct Node | ||
{ | ||
int data; | ||
Node *left; | ||
Node *right; | ||
}; | ||
|
||
pair<Node *, Node *> preSuc(Node *root, int data) | ||
{ | ||
if (root == nullptr) | ||
return {}; | ||
else | ||
{ | ||
pair<Node *, Node *> p; | ||
if (root->data == data) | ||
{ | ||
if (root->left) | ||
{ | ||
Node *temp = root->left; | ||
while (temp->right) | ||
{ | ||
temp = temp->right; | ||
} | ||
p.first = temp; | ||
} | ||
if (root->right) | ||
{ | ||
Node *temp = root->right; | ||
while (temp->left) | ||
{ | ||
temp = temp->left; | ||
} | ||
p.second = temp; | ||
} | ||
} | ||
else if (root->data > data) | ||
{ | ||
p.second = root; | ||
preSuc(root->left, data); | ||
} | ||
else | ||
{ | ||
p.first = root; | ||
preSuc(root->right, data); | ||
} | ||
return p; | ||
} | ||
} |
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,23 @@ | ||
|
||
class Tree | ||
{ | ||
public: | ||
int val; | ||
Tree *left; | ||
Tree *right; | ||
}; | ||
|
||
int answer(Tree *root, int lo, int hi) | ||
{ | ||
if (root == nullptr) | ||
return 0; | ||
else | ||
{ | ||
if (root->val >= lo && root->val <= hi) | ||
return 1 + answer(root->left, lo, hi) + answer(root->right, lo, hi); | ||
else if (root->val < lo) | ||
return answer(root->right, lo, hi); | ||
else | ||
return answer(root->left, lo, hi); | ||
} | ||
} |
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
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
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,36 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
struct Node | ||
{ | ||
int data; | ||
struct Node *next; | ||
Node(int x) | ||
{ | ||
data = x; | ||
next = NULL; | ||
} | ||
}; | ||
|
||
Node *removeDuplicates(Node *head) | ||
{ | ||
// your code goes here | ||
unordered_set<int> st; | ||
Node *curr = head; | ||
Node *prev = nullptr; | ||
while (curr != nullptr) | ||
{ | ||
if (st.find(curr->data) != st.end()) | ||
{ | ||
prev->next = curr->next; | ||
delete curr; | ||
} | ||
else | ||
{ | ||
st.insert(curr->data); | ||
prev = curr; | ||
} | ||
curr = prev->next; | ||
} | ||
return head; | ||
} |
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,37 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
struct Node | ||
{ | ||
int data; | ||
struct Node *next; | ||
Node(int x) | ||
{ | ||
data = x; | ||
next = NULL; | ||
} | ||
}; | ||
|
||
//Function to remove duplicates from sorted linked list. | ||
Node *removeDuplicates(Node *head) | ||
{ | ||
// your code goes here | ||
if (head == nullptr) | ||
return nullptr; | ||
Node *curr = head; | ||
Node *next; | ||
while (curr->next != nullptr) | ||
{ | ||
if (curr->data == curr->next->data) | ||
{ | ||
next = curr->next->next; | ||
free(curr->next); | ||
curr->next = next; | ||
} | ||
else | ||
{ | ||
curr = curr->next; | ||
} | ||
} | ||
return head; | ||
} |