diff --git a/1]. DSA + CP/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/_10)_Delete-Node-in-a-BST.cpp b/1]. DSA + CP/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/_10)_Delete-Node-in-a-BST.cpp new file mode 100644 index 000000000..3e85523b0 --- /dev/null +++ b/1]. DSA + CP/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/_10)_Delete-Node-in-a-BST.cpp @@ -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; + } +}; diff --git a/1]. DSA + CP/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/_11)_Insert-in-BST.cpp b/1]. DSA + CP/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/_11)_Insert-in-BST.cpp new file mode 100644 index 000000000..b82e4484f --- /dev/null +++ b/1]. DSA + CP/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/_11)_Insert-in-BST.cpp @@ -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; + } +}; \ No newline at end of file diff --git a/1]. DSA + CP/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/_12)_LCA-of-a-BST.cpp b/1]. DSA + CP/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/_12)_LCA-of-a-BST.cpp new file mode 100644 index 000000000..cc0ccc77a --- /dev/null +++ b/1]. DSA + CP/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/_12)_LCA-of-a-BST.cpp @@ -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; + } + +}; \ No newline at end of file diff --git a/1]. DSA + CP/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/_13)_Search-in-BST.cpp b/1]. DSA + CP/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/_13)_Search-in-BST.cpp new file mode 100644 index 000000000..bad51bdc4 --- /dev/null +++ b/1]. DSA + CP/1]. DSA/1]. Data Structures/13]. Binary Search Tree/C++/_13)_Search-in-BST.cpp @@ -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 {}; + + } +}; \ No newline at end of file