-
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
353 additions
and
40 deletions.
There are no files selected for viewing
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 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
struct Node | ||
{ | ||
int data; | ||
Node *left; | ||
Node *right; | ||
}; | ||
|
||
bool isBSTUtil(Node *root, int min, int max) | ||
{ | ||
if (root == nullptr) | ||
return true; | ||
if (root->data < min || root->data > max) | ||
return 0; | ||
return isBSTUtil(root->left, min, root->data - 1) && isBSTUtil(root->right, root->data + 1, max); | ||
} | ||
|
||
bool isBST(Node *root) | ||
{ | ||
return isBSTUtil(root, INT_MIN, INT_MAX); | ||
} |
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,40 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
struct Node | ||
{ | ||
int data; | ||
Node *left; | ||
Node *right; | ||
}; | ||
|
||
void inorder(Node *root, int k, int &c, int &ans) | ||
{ | ||
if (root == nullptr) | ||
return; | ||
else | ||
{ | ||
inorder(root->right, k, c, ans); | ||
c++; | ||
if (c == k) | ||
{ | ||
ans = root->data; | ||
return; | ||
} | ||
inorder(root->left, k, c, ans); | ||
} | ||
} | ||
|
||
int kthLargest(Node *root, int k) | ||
{ | ||
//Your code here | ||
if (root == nullptr) | ||
return 0; | ||
else | ||
{ | ||
int ans = 0; | ||
int n = 0; | ||
inorder(root, k, n, ans); | ||
return ans; | ||
} | ||
} |
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,72 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
struct ListNode | ||
{ | ||
int data; | ||
ListNode *next; | ||
}; | ||
|
||
struct Node | ||
{ | ||
ListNode *curr; | ||
int idx; | ||
Node(ListNode *c, int data) | ||
{ | ||
curr = c; | ||
idx = data; | ||
} | ||
}; | ||
|
||
struct compare | ||
{ | ||
bool operator()(Node *a, Node *b) | ||
{ | ||
return a->curr->data > b->curr->data; | ||
} | ||
}; | ||
|
||
ListNode *mergeKSortedList(vector<vector<ListNode *>> &lists) | ||
{ | ||
int k = lists.size(); | ||
if (k == 0) | ||
return nullptr; | ||
else | ||
{ | ||
ListNode *head, *tail; | ||
head = tail = NULL; | ||
priority_queue<Node, vector<Node>, compare> pq; | ||
vector<ListNode *> ptr(k); | ||
for (int i = 0; i < k; i++) | ||
{ | ||
ptr[i] = lists[i][0]; | ||
if (ptr[i] != nullptr) | ||
{ | ||
pq.push(Node(ptr[i], i)); | ||
} | ||
} | ||
if (pq.empty()) | ||
return nullptr; | ||
head = tail = pq.top().curr; | ||
int idx = pq.top().idx; | ||
pq.pop(); | ||
ptr[idx] = ptr[idx]->next; | ||
if (ptr[idx] != nullptr) | ||
{ | ||
pq.push(Node(ptr[idx], idx)); | ||
} | ||
while (!pq.empty()) | ||
{ | ||
ListNode *temp = pq.top().curr; | ||
idx = pq.top().idx; | ||
pq.pop(); | ||
tail->next = temp; | ||
tail = tail->next; | ||
ptr[idx] = ptr[idx]->next; | ||
if (ptr[idx] != nullptr) | ||
{ | ||
pq.push(Node(ptr[idx], idx)); | ||
} | ||
} | ||
} | ||
} |
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,60 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
struct Node | ||
{ | ||
int data; | ||
Node *next; | ||
Node(int val) | ||
{ | ||
data = val; | ||
next = nullptr; | ||
} | ||
}; | ||
|
||
Node *top = NULL; | ||
|
||
void push(int data) | ||
{ | ||
Node *temp = new Node(data); | ||
if (!temp) | ||
{ | ||
cout << "\nHeap Overflow" << endl; | ||
return; | ||
} | ||
temp->next = top; | ||
top = temp; | ||
} | ||
|
||
bool isEmpty() | ||
{ | ||
return top == nullptr; | ||
} | ||
|
||
int peek() | ||
{ | ||
if (!isEmpty()) | ||
{ | ||
return top->data; | ||
} | ||
else | ||
{ | ||
return -1; | ||
} | ||
} | ||
|
||
void pop() | ||
{ | ||
Node *temp; | ||
if (top == nullptr) | ||
{ | ||
cout << "\nStack Underflow" << endl; | ||
return; | ||
} | ||
else | ||
{ | ||
temp = top; | ||
top = top->next; | ||
delete temp; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,39 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
struct Node | ||
{ | ||
int data; | ||
Node *next; | ||
Node(int val) | ||
{ | ||
data = val; | ||
next = nullptr; | ||
} | ||
}; | ||
|
||
Node *front = nullptr; | ||
Node *rear = nullptr; | ||
|
||
void enQueue(int data) | ||
{ | ||
Node *temp = new Node(data); | ||
if (rear == nullptr) | ||
{ | ||
front = rear = temp; | ||
return; | ||
} | ||
rear->next = temp; | ||
rear = temp; | ||
} | ||
|
||
void deQueue() | ||
{ | ||
if (front == nullptr) | ||
return; | ||
Node *temp = front; | ||
front = front->next; | ||
if (front == nullptr) | ||
rear = nullptr; | ||
delete temp; | ||
} |
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,34 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
struct Node | ||
{ | ||
int data; | ||
Node *next; | ||
}; | ||
|
||
void sortList(Node *root) | ||
{ | ||
vector<int> cnt(3, 0); | ||
Node *curr = root; | ||
while (curr != nullptr) | ||
{ | ||
cnt[curr->data]++; | ||
curr = curr->next; | ||
} | ||
int i = 0; | ||
curr = root; | ||
while (curr != nullptr) | ||
{ | ||
if (cnt[i] == 0) | ||
{ | ||
i++; | ||
} | ||
else | ||
{ | ||
curr->data = i; | ||
cnt[i]--; | ||
curr = curr->next; | ||
} | ||
} | ||
} |
Oops, something went wrong.