Skip to content

Commit

Permalink
200th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Sep 1, 2024
1 parent 8c48e2e commit c9d8d9e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,12 @@ Ace Coding Interview with 75 Qs
[199]: ./src/page-2/199.%20Binary%20Tree%20Right%20Side%20View/rightSideView.ts
[1161]: ./src/page-11/1161.%20Maximum%20Level%20Sum%20of%20a%20Binary%20Tree/maxLevelSum.ts

| Binary Search Tree | | |
| ----------------------------------- | -------- | ------ |
| 700. Search in a Binary Search Tree | Solution | Easy |
| 450. Delete Node in a BST | Solution | Medium |
| Binary Search Tree | | |
| ----------------------------------- | --------------- | ------ |
| 700. Search in a Binary Search Tree | [Solution][700] | Easy |
| 450. Delete Node in a BST | Solution | Medium |

[700]: ./src/page-7/700.%20Search%20in%20a%20Binary%20Search%20Tree/searchBST.ts

| Graphs - DFS | | |
| ------------------------------------------------------------ | -------- | ------ |
Expand Down
21 changes: 21 additions & 0 deletions src/page-7/700. Search in a Binary Search Tree/searchBST.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { stringify } from 'flatted';

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

import { searchBST } from './searchBST';

describe('700. Search in a Binary Search Tree', () => {
test('searchBST', () => {
{
const root = generateBinaryTree([4, 2, 7, 1, 3]);
const expected = generateBinaryTree([2, 1, 3]);
expect(stringify(searchBST(root, 2))).toBe(stringify(expected));
}

{
const root = generateBinaryTree([4, 2, 7, 1, 3]);
const expected = generateBinaryTree([]);
expect(stringify(searchBST(root, 5))).toBe(stringify(expected));
}
});
});
12 changes: 12 additions & 0 deletions src/page-7/700. Search in a Binary Search Tree/searchBST.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { TreeNode } from '~/utils/binary-tree';

type SearchBST = (root: TreeNode | null, val: number) => TreeNode | null;

/**
* Accepted
*/
export const searchBST: SearchBST = (root, val) => {
if (!root || root.val === val) return root;
if (val < root.val) return searchBST(root.left, val);
return searchBST(root.right, val);
};

0 comments on commit c9d8d9e

Please sign in to comment.