Skip to content

Commit

Permalink
201st Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Sep 7, 2024
1 parent c9d8d9e commit 3e7aa6e
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,10 @@ Ace Coding Interview with 75 Qs
| Binary Search Tree | | |
| ----------------------------------- | --------------- | ------ |
| 700. Search in a Binary Search Tree | [Solution][700] | Easy |
| 450. Delete Node in a BST | Solution | Medium |
| 450. Delete Node in a BST | [Solution][450] | Medium |

[700]: ./src/page-7/700.%20Search%20in%20a%20Binary%20Search%20Tree/searchBST.ts
[450]: ./src/page-5/450.%20Delete%20Node%20in%20a%20BST/deleteNode.ts

| Graphs - DFS | | |
| ------------------------------------------------------------ | -------- | ------ |
Expand Down
27 changes: 27 additions & 0 deletions src/page-5/450. Delete Node in a BST/deleteNode.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { stringify } from 'flatted';

import { generateBinaryTree } from '~/utils/binary-tree';

import { deleteNode } from './deleteNode';

describe('450. Delete Node in a BST', () => {
test('deleteNode', () => {
{
const root = generateBinaryTree([5, 3, 6, 2, 4, null, 7]);
const expected = generateBinaryTree([5, 4, 6, 2, null, null, 7]);
expect(stringify(deleteNode(root, 3))).toBe(stringify(expected));
}

{
const root = generateBinaryTree([5, 3, 6, 2, 4, null, 7]);
const expected = generateBinaryTree([5, 3, 6, 2, 4, null, 7]);
expect(stringify(deleteNode(root, 0))).toBe(stringify(expected));
}

{
const root = generateBinaryTree([]);
const expected = generateBinaryTree([]);
expect(stringify(deleteNode(root, 0))).toBe(stringify(expected));
}
});
});
50 changes: 50 additions & 0 deletions src/page-5/450. Delete Node in a BST/deleteNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { TreeNode } from '~/utils/binary-tree';

type DeleteNode = (root: TreeNode | null, key: number) => TreeNode | null;

/**
* Accepted
*/
export const deleteNode: DeleteNode = (root, key) => {
// Base case: If the root is null, the tree is empty, or we've reached a leaf node.
if (root === null) return null;

// If the key to be deleted is less than the current node's value,
// search in the left subtree.
if (key < root.val) {
root.left = deleteNode(root.left, key);
return root; // Return the unchanged root after deletion in the left subtree.
}

// If the key to be deleted is greater than the current node's value,
// search in the right subtree.
if (key > root.val) {
root.right = deleteNode(root.right, key);
return root; // Return the unchanged root after deletion in the right subtree.
}

// If the key matches the current node's value, this is the node to be deleted.

// Case 1: Node has no left child, return the right child.
if (root.left === null) return root.right;

// Case 2: Node has no right child, return the left child.
if (root.right === null) return root.left;

// Case 3: Node has two children.
// Find the inorder successor (smallest node in the right subtree).
let minNode = root.right;

while (minNode.left !== null) {
minNode = minNode.left; // Traverse to the leftmost node.
}

// Replace the current node's value with the inorder successor's value.
root.val = minNode.val;

// Delete the inorder successor node (which is guaranteed to have at most one child).
root.right = deleteNode(root.right, minNode.val);

// Return the root of the updated tree.
return root;
};

0 comments on commit 3e7aa6e

Please sign in to comment.