Skip to content

Commit

Permalink
Update repo By Shubhanshu
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhanshurav authored Dec 30, 2022
1 parent 86890a6 commit 5822582
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* 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:

//find Inorder Successor
TreeNode *getSuccessor(TreeNode *curr){
curr = curr->right;
while(curr != NULL && curr->left != NULL){
curr = curr->left;
}
return curr;
}

TreeNode* deleteNode(TreeNode* root, int key) {

if(root == NULL) return NULL;

if(root->val > key){
root->left = deleteNode(root->left,key);
}else if(root->val < key){
root->right = deleteNode(root->right,key);
}else{
//node only with one child
if(root->left == NULL){
TreeNode *temp = root->right;
delete(root);
return temp;
}else if(root->right == NULL){
TreeNode *temp = root->left;
delete(root);
return temp;
}else{
//Node with both the childs : find Inorder successor
TreeNode *succ = getSuccessor(root);
root->val = succ->val;
root->right = deleteNode(root->right,succ->val);
}
}
return root;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* 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:

/*<............... Recursive Solution .......................>
TreeNode* insertIntoBST(TreeNode* root, int val) {
if(root == NULL)
return new TreeNode(val);
if(root->val > val){
root->left = insertIntoBST(root->left,val);
}else if(root->val < val){
root->right = insertIntoBST(root->right,val);
}
return root;
}
*/

//<............... Iterative Solution .......................>

TreeNode* insertIntoBST(TreeNode* root, int val) {

TreeNode *temp = new TreeNode(val);
TreeNode *curr = root, *parent = NULL;

while(curr != NULL){
parent = curr;

if(curr->val > val){
curr = curr -> left;
}else if(curr->val < val){
curr = curr -> right;
}else{
return root;
}
}

if(parent == NULL){
return temp;
}

if(parent->val > val){
parent->left = temp;
}else if(parent->val < val){
parent->right = temp;
}

return root;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/

class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {

if(root == NULL)
return NULL;

if(root->val > p->val && root->val > q->val){

return lowestCommonAncestor(root->left,p,q);

}else if(root->val < p->val && root->val < q->val){

return lowestCommonAncestor(root->right,p,q);

}

return root;
}

};
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* 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:

// <................> Approach - 01 <................>(Recurisive Approach)
/*
TreeNode* searchBST(TreeNode* root, int val) {
if(root == NULL) return {};
if(root->val == val) return root;
if(root->val > val){
return searchBST(root->left,val);
}else{
return searchBST(root->right,val);
}
}
*/

// <................> Approach - 02 <................>(Iterative Approach)

TreeNode* searchBST(TreeNode* root, int val) {

while(root!=NULL){

if(root -> val == val){
return root;
}else if(root->val > val){
root = root->left;
}else{
root = root->right;
}
}

return {};

}
};

0 comments on commit 5822582

Please sign in to comment.