Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.

Basically, the deletion can be divided into two stages:


	Search for a node to remove.
	If the node is found, delete the node.


&nbsp;
Example 1:

Input: root = [5,3,6,2,4,null,7], key = 3
Output: [5,4,6,2,null,null,7]
Explanation: Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.



Example 2:

Input: root = [5,3,6,2,4,null,7], key = 0
Output: [5,3,6,2,4,null,7]
Explanation: The tree does not contain a node with value = 0.


Example 3:

Input: root = [], key = 0
Output: []


&nbsp;
Constraints:


	The number of nodes in the tree is in the range [0, 104].
	-105 &lt;= Node.val &lt;= 105
	Each node has a unique value.
	root is a valid binary search tree.
	-105 &lt;= key &lt;= 105


&nbsp;
Follow up: Could you solve it with time complexity O(height of tree)?

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:

def find_inorder(root, key):

	if root is None :
		return []

	return find_inorder(root.left, key) + [root.val] + find_inorder(root.right, key)

def find_preorder(root, key):

	if root is None:
		return []

	return [root.val] + find_preorder(root.left,key) + find_preorder(root.right, key)

preorder = find_preorder(root, key)

try:
	preorder.remove(key)
except:
	return root

inorder = find_inorder(root, key)

inorder.remove(key)




hashmap = {}

for i in range(len(inorder)):
	key = inorder[i]
	hashmap[key] = i

def buildTree(left, right):

	if left > right:
		return 

	val = inorder[left]
	root = TreeNode(val)

	index = hashmap[val]

	root.left = buildTree(left, index-1)
	root.right = buildTree(index+1, right)

	return root

N = len(inorder)
new_tree = buildTree(0,N-1)

return new_tree
class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {
        if(root==null) return null;
        
        if(key<root.val){                            
            root.left = deleteNode(root.left,key);
            return root;
        }
        
        else if(key>root.val){
            root.right = deleteNode(root.right,key);
            return root;
        }
        
        else{
            if(root.left==null){
                return root.right;
            }
            else if(root.right==null){
                return root.left;
            }
            else{
                TreeNode min = root.right;
                while(min.left!=null){
                    min = min.left;
                }
                
                root.val = min.val;
                root.right = deleteNode(root.right,min.val);
                return root;
            }
        }
    }
}
class Solution {
public:
    TreeNode* deleteNode(TreeNode* root, int key) {
        if(root) 
            if(key < root->val) root->left = deleteNode(root->left, key);     //We frecursively call the function until we find the target node
            else if(key > root->val) root->right = deleteNode(root->right, key);       
            else{
                if(!root->left && !root->right) return NULL;          //No child condition
                if (!root->left || !root->right)
                    return root->left ? root->left : root->right;    //One child contion -> replace the node with it's child
					                                                //Two child condition   
                TreeNode* temp = root->left;                        //(or) TreeNode *temp = root->right;
                while(temp->right != NULL) temp = temp->right;     //      while(temp->left != NULL) temp = temp->left;
                root->val = temp->val;                            //       root->val = temp->val;
                root->left = deleteNode(root->left, temp->val);  //        root->right = deleteNode(root->right, temp);		
            }
        return root;
    }   
};
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */

const getLeftMostNode=(root)=>{
    if(!root)return null;
    let node=getLeftMostNode(root.left);
    return node?node:root;
}
/**
 * @param {TreeNode} root
 * @param {number} key
 * @return {TreeNode}
 */


var deleteNode = function(root, key) {
    if(!root)return null;
    if(root.val>key){
    root.left=deleteNode(root.left,key);
    }else if(root.val<key){
    root.right= deleteNode(root.right,key);
    }else{
        if(!root.left)return root.right;
        if(!root.right)return root.left;
        let succ_node=getLeftMostNode(root.right);
        root.val=succ_node.val;
        root.right= deleteNode(root.right,succ_node.val);
        
    }
    return root;

};
Delete Node in a BST