Skip to content

Commit

Permalink
add khushal87 new codes
Browse files Browse the repository at this point in the history
  • Loading branch information
khushal87 committed Mar 8, 2021
1 parent 02095e4 commit 4d4ef2f
Show file tree
Hide file tree
Showing 17 changed files with 260 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "C:/Program Files/CodeBlocks/MinGW/bin/g++.exe",
"compilerPath": "C:/TDM-GCC-32/bin/g++.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x64"
Expand Down
5 changes: 4 additions & 1 deletion .vscode/settings.json
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"
}
}
28 changes: 28 additions & 0 deletions BST/kth_smallest_element_in_bst.cpp
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;
}
}
21 changes: 21 additions & 0 deletions BST/lca.cpp
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.
46 changes: 46 additions & 0 deletions Tree/construct_tree_from_inorder_and_postorder.cpp
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;
}
67 changes: 67 additions & 0 deletions Tree/inorder_predecessor.cpp
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;
}
}
}
67 changes: 67 additions & 0 deletions Tree/inorder_successor.cpp
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;
}
}
}
26 changes: 26 additions & 0 deletions Tree/lca.cpp
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;
}
12 changes: 0 additions & 12 deletions filename.cpp

This file was deleted.

0 comments on commit 4d4ef2f

Please sign in to comment.