-
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
17 changed files
with
260 additions
and
14 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
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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
{ | ||
"files.associations": { | ||
"conio.h": "c" | ||
"conio.h": "c", | ||
"array": "cpp", | ||
"string": "cpp", | ||
"string_view": "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,28 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
struct Node | ||
{ | ||
int data; | ||
Node *left; | ||
Node *right; | ||
}; | ||
|
||
int solve(Node *root, int &k) | ||
{ | ||
if (root == nullptr) | ||
return 0; | ||
else | ||
{ | ||
int left = solve(root->left, k); | ||
if (left) | ||
return left; | ||
k -= 1; | ||
if (k == 0) | ||
{ | ||
return root->data; | ||
} | ||
int right = solve(root->right, k); | ||
return right; | ||
} | ||
} |
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,21 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
struct Node | ||
{ | ||
int data; | ||
Node *left; | ||
Node *right; | ||
}; | ||
|
||
Node *lca(Node *root, int n1, int n2) | ||
{ | ||
//Your code here | ||
if (root == nullptr) | ||
return nullptr; | ||
if (root->data > n1 && root->data > n2) | ||
return lca(root->left, n1, n2); | ||
if (root->data < n1 && root->data < n2) | ||
return lca(root->right, n1, n2); | ||
return root; | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,46 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
struct Node | ||
{ | ||
int data; | ||
Node *left; | ||
Node *right; | ||
}; | ||
|
||
Node *newNode(int data) | ||
{ | ||
Node *temp = NULL; | ||
temp->data = data; | ||
temp->left = temp->right = NULL; | ||
return temp; | ||
} | ||
|
||
int search(vector<int> in, int start, int end, int x) | ||
{ | ||
for (int i = start; i <= end; i++) | ||
{ | ||
if (in[i] == x) | ||
{ | ||
return i; | ||
} | ||
} | ||
} | ||
|
||
Node *buildTree(vector<int> in, vector<int> pre, int start, int end) | ||
{ | ||
static int preIdx = 0; | ||
if (start > end) | ||
{ | ||
return nullptr; | ||
} | ||
Node *tNode = newNode(pre[preIdx++]); | ||
if (start == end) | ||
{ | ||
return tNode; | ||
} | ||
int idx = search(in, start, end, tNode->data); | ||
tNode->left = buildTree(in, pre, start, preIdx - 1); | ||
tNode->right = buildTree(in, pre, preIdx + 1, end); | ||
return tNode; | ||
} |
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,67 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
struct Node | ||
{ | ||
int data; | ||
Node *left; | ||
Node *right; | ||
}; | ||
|
||
Node *searchData(Node *root, int data) | ||
{ | ||
if (root == nullptr) | ||
return nullptr; | ||
else | ||
{ | ||
if (root->data == data) | ||
return root; | ||
else if (root->data > data) | ||
{ | ||
return searchData(root->left, data); | ||
} | ||
else | ||
{ | ||
return searchData(root->right, data); | ||
} | ||
} | ||
} | ||
|
||
int inorderPredecessor(Node *root, int data) | ||
{ | ||
if (root == nullptr) | ||
{ | ||
return -1; | ||
} | ||
else | ||
{ | ||
Node *p = searchData(root, data); | ||
if (p->left != nullptr) | ||
{ | ||
Node *temp = p->left; | ||
while (temp->right != nullptr) | ||
{ | ||
temp = temp->right; | ||
} | ||
return temp->data; | ||
} | ||
else | ||
{ | ||
Node *s = root; | ||
Node *ans = NULL; | ||
while (p->data != s->data) | ||
{ | ||
if (p->data > s->data) | ||
{ | ||
ans = s; | ||
s = s->right; | ||
} | ||
else | ||
{ | ||
s = s->left; | ||
} | ||
} | ||
return ans ? ans->data : 0; | ||
} | ||
} | ||
} |
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,67 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
struct Node | ||
{ | ||
int data; | ||
Node *left; | ||
Node *right; | ||
}; | ||
|
||
Node *searchData(Node *root, int data) | ||
{ | ||
if (root == nullptr) | ||
return nullptr; | ||
else | ||
{ | ||
if (root->data == data) | ||
return root; | ||
else if (root->data > data) | ||
{ | ||
return searchData(root->left, data); | ||
} | ||
else | ||
{ | ||
return searchData(root->right, data); | ||
} | ||
} | ||
} | ||
|
||
int inorderPredecessor(Node *root, int data) | ||
{ | ||
if (root == nullptr) | ||
{ | ||
return -1; | ||
} | ||
else | ||
{ | ||
Node *p = searchData(root, data); | ||
if (p->right != nullptr) | ||
{ | ||
Node *temp = p->right; | ||
while (temp->left != nullptr) | ||
{ | ||
temp = temp->left; | ||
} | ||
return temp->data; | ||
} | ||
else | ||
{ | ||
Node *s = root; | ||
Node *ans = NULL; | ||
while (p->data != s->data) | ||
{ | ||
if (p->data < s->data) | ||
{ | ||
ans = s; | ||
s = s->left; | ||
} | ||
else | ||
{ | ||
s = s->right; | ||
} | ||
} | ||
return ans ? ans->data : 0; | ||
} | ||
} | ||
} |
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,26 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
struct Node | ||
{ | ||
int data; | ||
Node *left; | ||
Node *right; | ||
}; | ||
|
||
Node *lca(Node *root, int n1, int n2) | ||
{ | ||
//Your code here | ||
if (root == nullptr) | ||
return nullptr; | ||
if (root->data == n1 || root->data == n2) | ||
{ | ||
return root; | ||
} | ||
Node *left = lca(root->left, n1, n2); | ||
Node *right = lca(root->right, n1, n2); | ||
if (left != nullptr && right != nullptr) | ||
return root; | ||
else | ||
return left ? left : right; | ||
} |
This file was deleted.
Oops, something went wrong.