-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
1 parent
86890a6
commit 5822582
Showing
4 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
... CP/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/_10)_Delete-Node-in-a-BST.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,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; | ||
} | ||
}; |
64 changes: 64 additions & 0 deletions
64
1]. DSA + CP/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/_11)_Insert-in-BST.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,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; | ||
} | ||
}; |
31 changes: 31 additions & 0 deletions
31
1]. DSA + CP/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/_12)_LCA-of-a-BST.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,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; | ||
} | ||
|
||
}; |
51 changes: 51 additions & 0 deletions
51
1]. DSA + CP/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/_13)_Search-in-BST.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,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 {}; | ||
|
||
} | ||
}; |