deleteNode = function(root, key) {
    if (!root) return root;

    if (key === root.val) {
        if (!root.left) return root.right;
        if (!root.right) return root.left;

        // find the smallest val in right bst
        let curr = root.right;
        while (curr.left) {
            curr = curr.left;
        }
        // change the curr value
        root.val = curr.val;

        root.right = deleteNode(root.right, root.val);

        return root;
    } 
    if (key < root.val) {
        root.left = deleteNode(root.left, key);
        return root;
    }
    root.right = deleteNode(root.right, key);
    return root;
}
@param {TreeNode} root
@param {number} key
@return {TreeNode}