diff --git a/solutions/binaryTreeInorderTraversal.test.ts b/solutions/binaryTreeInorderTraversal.test.ts index 06fadd8..06fc897 100644 --- a/solutions/binaryTreeInorderTraversal.test.ts +++ b/solutions/binaryTreeInorderTraversal.test.ts @@ -1,4 +1,4 @@ -import { createBinaryTreeNode } from "../utilities/TreeNode"; +import { createBinaryTreeNode } from "../testUtilities/BinaryTree"; import inorderTraversal from "./binaryTreeInorderTraversal"; describe("94. Binary Tree Inorder Traversal", () => { diff --git a/solutions/binaryTreeInorderTraversal.ts b/solutions/binaryTreeInorderTraversal.ts index 8e5e765..9d39c23 100644 --- a/solutions/binaryTreeInorderTraversal.ts +++ b/solutions/binaryTreeInorderTraversal.ts @@ -1,11 +1,13 @@ -import { TreeNode } from "../utilities/TreeNode"; +import { BinaryTreeNode } from "../types/BinaryTree"; // 94. Binary Tree Inorder Traversal // https://leetcode.com/problems/binary-tree-inorder-traversal/ -export default function inorderTraversal(root: TreeNode | null): T[] { +export default function inorderTraversal( + root: BinaryTreeNode | null +): T[] { const history: T[] = []; - function traverse(node: TreeNode | null): void { + function traverse(node: BinaryTreeNode | null): void { if (node === null) return; traverse(node.left); diff --git a/solutions/binaryTreeLevelOrderTraversal.test.ts b/solutions/binaryTreeLevelOrderTraversal.test.ts index 9065734..5bc229c 100644 --- a/solutions/binaryTreeLevelOrderTraversal.test.ts +++ b/solutions/binaryTreeLevelOrderTraversal.test.ts @@ -1,4 +1,4 @@ -import { createBinaryTreeNode } from "../utilities/TreeNode"; +import { createBinaryTreeNode } from "../testUtilities/BinaryTree"; import levelOrder from "./binaryTreeLevelOrderTraversal"; describe("102. Binary Tree Level Order Traversal", () => { diff --git a/solutions/binaryTreeLevelOrderTraversal.ts b/solutions/binaryTreeLevelOrderTraversal.ts index cb91a6d..d096604 100644 --- a/solutions/binaryTreeLevelOrderTraversal.ts +++ b/solutions/binaryTreeLevelOrderTraversal.ts @@ -1,11 +1,11 @@ -import { TreeNode } from "../utilities/TreeNode"; +import { BinaryTreeNode } from "../types/BinaryTree"; // 102. Binary Tree Level Order Traversal // https://leetcode.com/problems/binary-tree-level-order-traversal/ -export default function levelOrder(root: TreeNode | null): T[][] { +export default function levelOrder(root: BinaryTreeNode | null): T[][] { const valuesEachLevel: T[][] = []; - function traverse(node: TreeNode | null, level: number) { + function traverse(node: BinaryTreeNode | null, level: number) { if (node === null) return; if (valuesEachLevel[level]) { diff --git a/solutions/binaryTreePostorderTraversal.test.ts b/solutions/binaryTreePostorderTraversal.test.ts index f84e0e0..14f466d 100644 --- a/solutions/binaryTreePostorderTraversal.test.ts +++ b/solutions/binaryTreePostorderTraversal.test.ts @@ -1,4 +1,4 @@ -import { createBinaryTreeNode } from "../utilities/TreeNode"; +import { createBinaryTreeNode } from "../testUtilities/BinaryTree"; import postorderTraversal from "./binaryTreePostorderTraversal"; describe("145. Binary Tree Postorder Traversal", () => { diff --git a/solutions/binaryTreePostorderTraversal.ts b/solutions/binaryTreePostorderTraversal.ts index 4c63563..8cf3475 100644 --- a/solutions/binaryTreePostorderTraversal.ts +++ b/solutions/binaryTreePostorderTraversal.ts @@ -1,11 +1,13 @@ -import { TreeNode } from "../utilities/TreeNode"; +import { BinaryTreeNode } from "../types/BinaryTree"; // 145. Binary Tree Postorder Traversal // https://leetcode.com/problems/binary-tree-postorder-traversal/ -export default function postorderTraversal(root: TreeNode | null): T[] { +export default function postorderTraversal( + root: BinaryTreeNode | null +): T[] { const history: T[] = []; - function traverse(node: TreeNode | null): void { + function traverse(node: BinaryTreeNode | null): void { if (node === null) return; traverse(node.left); diff --git a/solutions/binaryTreePreorderTraversal.test.ts b/solutions/binaryTreePreorderTraversal.test.ts index bb3183b..b886a64 100644 --- a/solutions/binaryTreePreorderTraversal.test.ts +++ b/solutions/binaryTreePreorderTraversal.test.ts @@ -1,4 +1,4 @@ -import { createBinaryTreeNode } from "../utilities/TreeNode"; +import { createBinaryTreeNode } from "../testUtilities/BinaryTree"; import preorderTraversal from "./binaryTreePreorderTraversal"; describe("144. Binary Tree Preorder Traversal", () => { diff --git a/solutions/binaryTreePreorderTraversal.ts b/solutions/binaryTreePreorderTraversal.ts index 17cc99e..1756d4a 100644 --- a/solutions/binaryTreePreorderTraversal.ts +++ b/solutions/binaryTreePreorderTraversal.ts @@ -1,11 +1,13 @@ -import { TreeNode } from "../utilities/TreeNode"; +import { BinaryTreeNode } from "../types/BinaryTree"; // 144. Binary Tree Preorder Traversal // https://leetcode.com/problems/binary-tree-preorder-traversal/ -export default function preorderTraversal(root: TreeNode | null): T[] { +export default function preorderTraversal( + root: BinaryTreeNode | null +): T[] { const history: T[] = []; - function traverse(node: TreeNode | null): void { + function traverse(node: BinaryTreeNode | null): void { if (node === null) return; history.push(node.val); diff --git a/solutions/countCompleteTreeNodes.test.ts b/solutions/countCompleteTreeNodes.test.ts index 374085b..7b25686 100644 --- a/solutions/countCompleteTreeNodes.test.ts +++ b/solutions/countCompleteTreeNodes.test.ts @@ -1,4 +1,4 @@ -import { createBinaryTreeNode } from "../utilities/TreeNode"; +import { createBinaryTreeNode } from "../testUtilities/BinaryTree"; import countNodes from "./countCompleteTreeNodes"; describe("222. Count Complete Tree Nodes", () => { diff --git a/solutions/countCompleteTreeNodes.ts b/solutions/countCompleteTreeNodes.ts index 77db2aa..2d61635 100644 --- a/solutions/countCompleteTreeNodes.ts +++ b/solutions/countCompleteTreeNodes.ts @@ -1,8 +1,8 @@ -import { TreeNode } from "../utilities/TreeNode"; +import { BinaryTreeNode } from "../types/BinaryTree"; // 222. Count Complete Tree Nodes // https://leetcode.com/problems/count-complete-tree-nodes/ -function countNodes(root: TreeNode | null): number { +function countNodes(root: BinaryTreeNode | null): number { if (!root) return 0; const leftHeight = calculateHeight(root.left); @@ -17,7 +17,7 @@ function countNodes(root: TreeNode | null): number { 2 ** rightHeight + countNodes(root.left); } -function calculateHeight(root: TreeNode | null): number { +function calculateHeight(root: BinaryTreeNode | null): number { return root ? 1 + calculateHeight(root.left) : 0; } diff --git a/solutions/countUnivalueSubtrees.test.ts b/solutions/countUnivalueSubtrees.test.ts index 3675d7c..c0a5371 100644 --- a/solutions/countUnivalueSubtrees.test.ts +++ b/solutions/countUnivalueSubtrees.test.ts @@ -1,4 +1,4 @@ -import { createBinaryTreeNode } from "../utilities/TreeNode"; +import { createBinaryTreeNode } from "../testUtilities/BinaryTree"; import countUnivalSubtrees from "./countUnivalueSubtrees"; describe("250. Count Univalue Subtrees", () => { diff --git a/solutions/countUnivalueSubtrees.ts b/solutions/countUnivalueSubtrees.ts index eaa4a0a..ad74e79 100644 --- a/solutions/countUnivalueSubtrees.ts +++ b/solutions/countUnivalueSubtrees.ts @@ -1,11 +1,13 @@ -import { TreeNode } from "../utilities/TreeNode"; +import { BinaryTreeNode } from "../types/BinaryTree"; // 250. Count Univalue Subtrees // https://leetcode.com/problems/count-univalue-subtrees/ export default function countUnivalSubtrees( - root: TreeNode | null + root: BinaryTreeNode | null ): number { - function traverse(node: TreeNode | null): [number, number | null] { + function traverse( + node: BinaryTreeNode | null + ): [number, number | null] { if (node === null) return [0, null]; let [leftCount, leftValue] = traverse(node.left); diff --git a/solutions/diameterOfBinaryTree.test.ts b/solutions/diameterOfBinaryTree.test.ts index 4febc56..93b12c7 100644 --- a/solutions/diameterOfBinaryTree.test.ts +++ b/solutions/diameterOfBinaryTree.test.ts @@ -1,4 +1,4 @@ -import { createBinaryTreeNode } from "../utilities/TreeNode"; +import { createBinaryTreeNode } from "../testUtilities/BinaryTree"; import diameterOfBinaryTree from "./diameterOfBinaryTree"; describe("543. Diameter of Binary Tree", () => { diff --git a/solutions/diameterOfBinaryTree.ts b/solutions/diameterOfBinaryTree.ts index eeb4922..834805a 100644 --- a/solutions/diameterOfBinaryTree.ts +++ b/solutions/diameterOfBinaryTree.ts @@ -1,11 +1,13 @@ -import { TreeNode } from "../utilities/TreeNode"; +import { BinaryTreeNode } from "../types/BinaryTree"; // 543. Diameter of Binary Tree // https://leetcode.com/problems/diameter-of-binary-tree/ export default function diameterOfBinaryTree( - node: TreeNode | null + node: BinaryTreeNode | null ): number { - function getHeightAndDiameter(node: TreeNode | null): [number, number] { + function getHeightAndDiameter( + node: BinaryTreeNode | null + ): [number, number] { if (node === null) return [-1, -1]; const [leftHeight, leftDiameter] = getHeightAndDiameter(node.left); diff --git a/solutions/linkedListCycle.test.ts b/solutions/linkedListCycle.test.ts index f1dd835..228cbf4 100644 --- a/solutions/linkedListCycle.test.ts +++ b/solutions/linkedListCycle.test.ts @@ -1,4 +1,5 @@ -import { createListNode, ListNode } from "../utilities/ListNode"; +import { createSinglyLinkedListNode } from "../testUtilities/LinkedList"; +import { SinglyLinkedListNode } from "../types/LinkedList"; import hasCycle from "./linkedListCycle"; describe("141. Linked List Cycle", () => { @@ -10,10 +11,10 @@ describe("141. Linked List Cycle", () => { for (const [[values, cycleStart], expected] of TEST_CASES) { it(`returns ${expected} when called with [${values}] (cycle starts from ${cycleStart})`, () => { - const head = createListNode(values); + const head = createSinglyLinkedListNode(values); let node = head; - let cycleStartNode: ListNode | null = null; + let cycleStartNode: SinglyLinkedListNode | null = null; for (const i of values.keys()) { if (i === cycleStart) { diff --git a/solutions/linkedListCycle.ts b/solutions/linkedListCycle.ts index 6b742a2..dd26497 100644 --- a/solutions/linkedListCycle.ts +++ b/solutions/linkedListCycle.ts @@ -1,11 +1,13 @@ -import { ListNode } from "../utilities/ListNode"; +import { SinglyLinkedListNode } from "../types/LinkedList"; // 141. Linked List Cycle // https://leetcode.com/problems/linked-list-cycle/ -export default function hasCycle(head: ListNode | null): boolean { +export default function hasCycle( + head: SinglyLinkedListNode | null +): boolean { function traverse( - previous: ListNode | null, - node: ListNode | null + previous: SinglyLinkedListNode | null, + node: SinglyLinkedListNode | null ): boolean { if (node === null) return false; if (node === head && previous !== null) return true; diff --git a/solutions/maximumDepthOfBinaryTree.test.ts b/solutions/maximumDepthOfBinaryTree.test.ts index 5b58232..350b76d 100644 --- a/solutions/maximumDepthOfBinaryTree.test.ts +++ b/solutions/maximumDepthOfBinaryTree.test.ts @@ -1,4 +1,4 @@ -import { createBinaryTreeNode } from "../utilities/TreeNode"; +import { createBinaryTreeNode } from "../testUtilities/BinaryTree"; import maxDepth from "./maximumDepthOfBinaryTree"; describe("104. Maximum Depth of Binary Tree", () => { diff --git a/solutions/maximumDepthOfBinaryTree.ts b/solutions/maximumDepthOfBinaryTree.ts index 0df2f66..bb1ace3 100644 --- a/solutions/maximumDepthOfBinaryTree.ts +++ b/solutions/maximumDepthOfBinaryTree.ts @@ -1,8 +1,8 @@ -import { TreeNode } from "../utilities/TreeNode"; +import { BinaryTreeNode } from "../types/BinaryTree"; // 104. Maximum Depth of Binary Tree // https://leetcode.com/problems/maximum-depth-of-binary-tree/ -export default function maxDepth(node: TreeNode | null): number { +export default function maxDepth(node: BinaryTreeNode | null): number { return node === null ? 0 : Math.max(maxDepth(node.left), maxDepth(node.right)) + 1; diff --git a/solutions/mergeTwoLists.test.ts b/solutions/mergeTwoLists.test.ts index da9b25e..6ba0818 100644 --- a/solutions/mergeTwoLists.test.ts +++ b/solutions/mergeTwoLists.test.ts @@ -1,4 +1,4 @@ -import { createListNode } from "../utilities/ListNode"; +import { createSinglyLinkedListNode } from "../testUtilities/LinkedList"; import mergeTwoLists from "./mergeTwoLists"; describe("21. Merge Two Sorted Lists", () => { @@ -10,8 +10,11 @@ describe("21. Merge Two Sorted Lists", () => { for (const [[list1, list2], expected] of TEST_CASES) { it(`returns [${expected}] when called with [${list1}] and [${list2}]`, () => { expect( - mergeTwoLists(createListNode(list1), createListNode(list2)) - ).toEqual(createListNode(expected)); + mergeTwoLists( + createSinglyLinkedListNode(list1), + createSinglyLinkedListNode(list2) + ) + ).toEqual(createSinglyLinkedListNode(expected)); }); } }); diff --git a/solutions/mergeTwoLists.ts b/solutions/mergeTwoLists.ts index f294499..dfee38a 100644 --- a/solutions/mergeTwoLists.ts +++ b/solutions/mergeTwoLists.ts @@ -1,11 +1,11 @@ -import { ListNode } from "../utilities/ListNode"; +import { SinglyLinkedListNode } from "../types/LinkedList"; // 21. Merge Two Sorted Lists // https://leetcode.com/problems/merge-two-sorted-lists/ function mergeTwoLists( - list1: ListNode | null, - list2: ListNode | null -): ListNode | null { + list1: SinglyLinkedListNode | null, + list2: SinglyLinkedListNode | null +): SinglyLinkedListNode | null { if (!list1) { return list2; } diff --git a/solutions/minimumDepthOfBinaryTree.test.ts b/solutions/minimumDepthOfBinaryTree.test.ts index cf69770..2e544a8 100644 --- a/solutions/minimumDepthOfBinaryTree.test.ts +++ b/solutions/minimumDepthOfBinaryTree.test.ts @@ -1,4 +1,4 @@ -import { createBinaryTreeNode } from "../utilities/TreeNode"; +import { createBinaryTreeNode } from "../testUtilities/BinaryTree"; import minDepth from "./minimumDepthOfBinaryTree"; describe("111. Minimum Depth of Binary Tree", () => { diff --git a/solutions/minimumDepthOfBinaryTree.ts b/solutions/minimumDepthOfBinaryTree.ts index 68f64e8..86d41a3 100644 --- a/solutions/minimumDepthOfBinaryTree.ts +++ b/solutions/minimumDepthOfBinaryTree.ts @@ -1,9 +1,9 @@ -import { TreeNode } from "../utilities/TreeNode"; +import { BinaryTreeNode } from "../types/BinaryTree"; // 111. Minimum Depth of Binary Tree // https://leetcode.com/problems/minimum-depth-of-binary-tree/ -export default function minDepth(node: TreeNode | null) { - const queue: [TreeNode | null, number][] = [[node, 1]]; +export default function minDepth(node: BinaryTreeNode | null) { + const queue: [BinaryTreeNode | null, number][] = [[node, 1]]; while (queue.length > 0) { const [node, depth] = queue.shift()!; diff --git a/solutions/palindromeLinkedList.test.ts b/solutions/palindromeLinkedList.test.ts index f0cd394..a13a896 100644 --- a/solutions/palindromeLinkedList.test.ts +++ b/solutions/palindromeLinkedList.test.ts @@ -1,4 +1,4 @@ -import { createListNode } from "../utilities/ListNode"; +import { createSinglyLinkedListNode } from "../testUtilities/LinkedList"; import isPalindrome from "./palindromeLinkedList"; describe("234. Palindrome Linked List", () => { @@ -11,7 +11,7 @@ describe("234. Palindrome Linked List", () => { for (const [values, expected] of TEST_CASES) { it(`returns ${expected} when called with [${values}]`, () => { - expect(isPalindrome(createListNode(values))).toBe(expected); + expect(isPalindrome(createSinglyLinkedListNode(values))).toBe(expected); }); } }); diff --git a/solutions/palindromeLinkedList.ts b/solutions/palindromeLinkedList.ts index 74198a4..d1e689a 100644 --- a/solutions/palindromeLinkedList.ts +++ b/solutions/palindromeLinkedList.ts @@ -1,11 +1,13 @@ -import { ListNode } from "../utilities/ListNode"; +import { SinglyLinkedListNode } from "../types/LinkedList"; // 234. Palindrome Linked List // https://leetcode.com/problems/palindrome-linked-list/ -export default function isPalindrome(node: ListNode | null): boolean { +export default function isPalindrome( + node: SinglyLinkedListNode | null +): boolean { let left = node; - function traverse(right: ListNode | null): boolean { + function traverse(right: SinglyLinkedListNode | null): boolean { if (right === null) return true; const isNextMightBePalindrome = traverse(right.next); diff --git a/solutions/pathSum.test.ts b/solutions/pathSum.test.ts index e871a5f..4a6142d 100644 --- a/solutions/pathSum.test.ts +++ b/solutions/pathSum.test.ts @@ -1,4 +1,4 @@ -import { createBinaryTreeNode } from "../utilities/TreeNode"; +import { createBinaryTreeNode } from "../testUtilities/BinaryTree"; import pathSum from "./pathSum"; describe("112. Path Sum", () => { diff --git a/solutions/pathSum.ts b/solutions/pathSum.ts index caf48cc..e6877da 100644 --- a/solutions/pathSum.ts +++ b/solutions/pathSum.ts @@ -1,9 +1,9 @@ -import { TreeNode } from "../utilities/TreeNode"; +import { BinaryTreeNode } from "../types/BinaryTree"; // 112. Path Sum // https://leetcode.com/problems/path-sum/ export default function hasPathSum( - root: TreeNode | null, + root: BinaryTreeNode | null, sum: number ): boolean { // ⚠️ as following the test cases, we need to return false for hasPathSum(null, 0) diff --git a/solutions/reverseLinkedList.test.ts b/solutions/reverseLinkedList.test.ts index 36c456f..9b89e11 100644 --- a/solutions/reverseLinkedList.test.ts +++ b/solutions/reverseLinkedList.test.ts @@ -1,4 +1,4 @@ -import { createListNode } from "../utilities/ListNode"; +import { createSinglyLinkedListNode } from "../testUtilities/LinkedList"; import reverseList from "./reverseLinkedList"; describe("206. Reverse Linked List", () => { @@ -11,8 +11,8 @@ describe("206. Reverse Linked List", () => { for (const [values, expected] of TEST_CASES) { it(`returns [${expected}] when called with [${values}]`, () => { - expect(reverseList(createListNode(values))).toEqual( - createListNode(expected) + expect(reverseList(createSinglyLinkedListNode(values))).toEqual( + createSinglyLinkedListNode(expected) ); }); } diff --git a/solutions/reverseLinkedList.ts b/solutions/reverseLinkedList.ts index 6e26660..15335b9 100644 --- a/solutions/reverseLinkedList.ts +++ b/solutions/reverseLinkedList.ts @@ -1,8 +1,10 @@ -import { ListNode } from "../utilities/ListNode"; +import { SinglyLinkedListNode } from "../types/LinkedList"; // 206. Reverse Linked List // https://leetcode.com/problems/reverse-linked-list/ -function reverseList(head: ListNode | null): ListNode | null { +function reverseList( + head: SinglyLinkedListNode | null +): SinglyLinkedListNode | null { if (head === null || head.next === null) return head; // call itself recursively with next node as the new head: [1, 2, 3] -> [2, 3] -> [3] diff --git a/solutions/subtreeOfAnotherTree.test.ts b/solutions/subtreeOfAnotherTree.test.ts index 88d5350..7dc4630 100644 --- a/solutions/subtreeOfAnotherTree.test.ts +++ b/solutions/subtreeOfAnotherTree.test.ts @@ -1,4 +1,4 @@ -import { createBinaryTreeNode } from "../utilities/TreeNode"; +import { createBinaryTreeNode } from "../testUtilities/BinaryTree"; import isSubtree from "./subtreeOfAnotherTree"; describe("572. Subtree of Another Tree", () => { diff --git a/solutions/subtreeOfAnotherTree.ts b/solutions/subtreeOfAnotherTree.ts index e5e4ea2..982e2bf 100644 --- a/solutions/subtreeOfAnotherTree.ts +++ b/solutions/subtreeOfAnotherTree.ts @@ -1,17 +1,20 @@ -import { TreeNode } from "../utilities/TreeNode"; +import { BinaryTreeNode } from "../types/BinaryTree"; // 572. Subtree of Another Tree // https://leetcode.com/problems/subtree-of-another-tree/ export default function isSubtree( - s: TreeNode | null, - t: TreeNode | null + s: BinaryTreeNode | null, + t: BinaryTreeNode | null ): boolean { return s === null ? s === t : isSameTree(s, t) || isSubtree(s.left, t) || isSubtree(s.right, t); } -function isSameTree(s: TreeNode | null, t: TreeNode | null): boolean { +function isSameTree( + s: BinaryTreeNode | null, + t: BinaryTreeNode | null +): boolean { if (s === null && t === null) return true; if (s !== null && t !== null) diff --git a/solutions/symmetricTree.test.ts b/solutions/symmetricTree.test.ts index 5fdd7ea..9f17f6d 100644 --- a/solutions/symmetricTree.test.ts +++ b/solutions/symmetricTree.test.ts @@ -1,4 +1,4 @@ -import { createBinaryTreeNode } from "../utilities/TreeNode"; +import { createBinaryTreeNode } from "../testUtilities/BinaryTree"; import isSymmetric from "./symmetricTree"; describe("101. Symmetric Tree", () => { diff --git a/solutions/symmetricTree.ts b/solutions/symmetricTree.ts index d5a1763..387e832 100644 --- a/solutions/symmetricTree.ts +++ b/solutions/symmetricTree.ts @@ -1,12 +1,12 @@ -import { TreeNode } from "../utilities/TreeNode"; +import { BinaryTreeNode } from "../types/BinaryTree"; // 101. Symmetric Tree // https://leetcode.com/problems/symmetric-tree/ -function isSymmetric(node: TreeNode | null): boolean { +function isSymmetric(node: BinaryTreeNode | null): boolean { // check recursively for each outside-elements function isEqual( - node1: TreeNode | null, - node2: TreeNode | null + node1: BinaryTreeNode | null, + node2: BinaryTreeNode | null ): boolean { // if both are null if (!node1 && !node2) return true; diff --git a/solutions/validateBinarySearchTree.test.ts b/solutions/validateBinarySearchTree.test.ts index 79d4d8f..a6f127e 100644 --- a/solutions/validateBinarySearchTree.test.ts +++ b/solutions/validateBinarySearchTree.test.ts @@ -1,4 +1,4 @@ -import { createBinaryTreeNode } from "../utilities/TreeNode"; +import { createBinaryTreeNode } from "../testUtilities/BinaryTree"; import isValidBST from "./validateBinarySearchTree"; describe("98. Validate Binary Search Tree", () => { diff --git a/solutions/validateBinarySearchTree.ts b/solutions/validateBinarySearchTree.ts index 3eaf9b3..d50d8e1 100644 --- a/solutions/validateBinarySearchTree.ts +++ b/solutions/validateBinarySearchTree.ts @@ -1,9 +1,9 @@ -import { TreeNode } from "../utilities/TreeNode"; +import { BinaryTreeNode } from "../types/BinaryTree"; // 98. Validate Binary Search Tree // https://leetcode.com/problems/validate-binary-search-tree/ export default function isValidBST( - node: TreeNode | null, + node: BinaryTreeNode | null, greaterThan = -Infinity, lessThan = Infinity ): boolean { diff --git a/utilities/TreeNode.test.ts b/testUtilities/BinaryTree.test.ts similarity index 85% rename from utilities/TreeNode.test.ts rename to testUtilities/BinaryTree.test.ts index 32a3130..08512d5 100644 --- a/utilities/TreeNode.test.ts +++ b/testUtilities/BinaryTree.test.ts @@ -1,7 +1,7 @@ -import { createBinaryTreeNode } from "./TreeNode"; +import { createBinaryTreeNode } from "./BinaryTree"; describe("createBinaryTreeNode", () => { - it("creates TreeNode by an array #1", () => { + it("creates BinaryTreeNode by an array #1", () => { expect(createBinaryTreeNode([1, 2, 3])).toEqual({ val: 1, left: { @@ -17,7 +17,7 @@ describe("createBinaryTreeNode", () => { }); }); - it("creates TreeNode by an array #2", () => { + it("creates BinaryTreeNode by an array #2", () => { expect(createBinaryTreeNode([1, null, 2, 3])).toEqual({ val: 1, left: null, @@ -33,7 +33,7 @@ describe("createBinaryTreeNode", () => { }); }); - it("creates TreeNode by an array #3", () => { + it("creates BinaryTreeNode by an array #3", () => { expect( createBinaryTreeNode([5, 4, 7, 3, null, 2, null, -1, null, 9]) ).toEqual({ diff --git a/utilities/TreeNode.ts b/testUtilities/BinaryTree.ts similarity index 82% rename from utilities/TreeNode.ts rename to testUtilities/BinaryTree.ts index f8f2c6f..9f98ecc 100644 --- a/utilities/TreeNode.ts +++ b/testUtilities/BinaryTree.ts @@ -1,17 +1,13 @@ -export interface TreeNode { - val: T; - left: TreeNode | null; - right: TreeNode | null; -} +import { BinaryTreeNode } from "../types/BinaryTree"; export function createBinaryTreeNode( array: (T | null)[] -): TreeNode | null { +): BinaryTreeNode | null { if (array.length === 0) return null; let skipped = 0; const root = { val: array[0]!, left: null, right: null }; - const queue: [number, TreeNode, BinaryTreeSide][] = [ + const queue: [number, BinaryTreeNode, BinaryTreeSide][] = [ [1, root, BinaryTreeSide.left], [2, root, BinaryTreeSide.right] ]; diff --git a/testUtilities/LinkedList.test.ts b/testUtilities/LinkedList.test.ts new file mode 100644 index 0000000..c9c2d7f --- /dev/null +++ b/testUtilities/LinkedList.test.ts @@ -0,0 +1,96 @@ +import { createSinglyLinkedListNode, getNthNode } from "./LinkedList"; + +describe("createSinglyLinkedListNode", () => { + test("creates SinglyLinkedListNode by an array #1", () => { + expect(createSinglyLinkedListNode([1, 2, 3, 4, 5, 6, 7])).toEqual({ + val: 1, + next: { + val: 2, + next: { + val: 3, + next: { + val: 4, + next: { + val: 5, + next: { + val: 6, + next: { + val: 7, + next: null + } + } + } + } + } + } + }); + }); + + test("creates SinglyLinkedListNode by an array #2", () => { + expect(createSinglyLinkedListNode([1])).toEqual({ + val: 1, + next: null + }); + }); + + test("returns null if the given array has no element", () => { + expect(createSinglyLinkedListNode([])).toBe(null); + }); +}); + +describe("getNthNode()", () => { + it("returns the first node in a linked list if called with 0 index", () => { + const head = createSinglyLinkedListNode([0, 1, 2, 3, 4, 5])!; + + expect(getNthNode(head, 0).val).toBe(0); + }); + + it("returns the 3rd node in a linked list if called with 2 index", () => { + const head = createSinglyLinkedListNode([0, 1, 2, 3, 4, 5])!; + + expect(getNthNode(head, 2).val).toBe(2); + }); + + it("returns the last node in a linked list if called with -1 index", () => { + const head = createSinglyLinkedListNode([0, 1, 2, 3, 4, 5])!; + + expect(getNthNode(head, -1)).toBe(getNthNode(head, 5)); + }); + + it("returns the 3rd node from the tail in a linked list if called with -3 index", () => { + const head = createSinglyLinkedListNode([0, 1, 2, 3, 4, 5])!; + + expect(getNthNode(head, -3)).toBe(getNthNode(head, 3)); + }); + + it("doesn't mutates the given linked list at all", () => { + const head = createSinglyLinkedListNode([0, 1, 2, 3, 4, 5])!; + + getNthNode(head, 1); + getNthNode(head, 2); + getNthNode(head, 3); + + expect(getNthNode(head, 2).val).toBe(2); + }); + + it("throws an Error if the index is out of bounds (when called with 7 for a 6-nodes linked list)", () => { + const head = createSinglyLinkedListNode([0, 1, 2, 3, 4, 5])!; + + expect(() => getNthNode(head, 7)).toThrow(Error); + }); + + it("throws an Error if the index is out of bounds (when called with -7 for a 6-nodes linked list)", () => { + const head = createSinglyLinkedListNode([0, 1, 2, 3, 4, 5])!; + + expect(() => getNthNode(head, -7)).toThrow(Error); + }); + + it("throws a TypeError if the index is not an integer", () => { + const head = createSinglyLinkedListNode([0, 1, 2, 3, 4, 5])!; + + expect(() => getNthNode(head, 1.1)).toThrow(TypeError); + expect(() => getNthNode(head, Infinity)).toThrow(TypeError); + expect(() => getNthNode(head, -Infinity)).toThrow(TypeError); + expect(() => getNthNode(head, NaN)).toThrow(TypeError); + }); +}); diff --git a/testUtilities/LinkedList.ts b/testUtilities/LinkedList.ts new file mode 100644 index 0000000..8cb60c4 --- /dev/null +++ b/testUtilities/LinkedList.ts @@ -0,0 +1,50 @@ +import { SinglyLinkedListNode } from "../types/LinkedList"; + +export function createSinglyLinkedListNode( + array: T[] +): SinglyLinkedListNode | null { + if (array.length === 0) return null; + + const head: SinglyLinkedListNode = { val: array[0], next: null }; + let tail = head; + + for (let i = 1; i < array.length; ++i) { + const node = { val: array[i], next: null }; + + tail.next = node; + tail = node; + } + + return head; +} + +export function getNthNode( + node: SinglyLinkedListNode, + n: number +): SinglyLinkedListNode { + if (!Number.isSafeInteger(n)) { + throw new TypeError("the given index is not an integer"); + } + + const isNegative = n < 0; + + const nodes = [node]; + let i = 0; + + while (isNegative || i <= n) { + const next = nodes[nodes.length - 1].next; + + if (!next) break; + + nodes.push(next); + i += 1; + } + + const actualN = isNegative ? nodes.length + n : n; + + if (nodes[actualN] === undefined) { + throw new Error("the given index is out of bounds"); + } + + return nodes[actualN]; +} diff --git a/types/BinaryTree.ts b/types/BinaryTree.ts new file mode 100644 index 0000000..761f86e --- /dev/null +++ b/types/BinaryTree.ts @@ -0,0 +1,5 @@ +export interface BinaryTreeNode { + val: T; + left: BinaryTreeNode | null; + right: BinaryTreeNode | null; +} diff --git a/types/LinkedList.ts b/types/LinkedList.ts new file mode 100644 index 0000000..d707bff --- /dev/null +++ b/types/LinkedList.ts @@ -0,0 +1,4 @@ +export interface SinglyLinkedListNode { + val: T; + next: SinglyLinkedListNode | null; +} diff --git a/utilities/Heapify.test.ts b/utilities/Heapify.test.ts deleted file mode 100644 index f740c84..0000000 --- a/utilities/Heapify.test.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { heapPop, heapPush } from "./Heapify"; - -describe("heapPush()", () => { - it("returns an array that its head value is most prior", () => { - expect(heapPush([3, 2, 1], 5)[0]).toBe(5); - expect(heapPush([3, 2, 1], 0)[0]).toBe(3); - }); - - it("returns an array that its head value is most prior even when an one-element array", () => { - expect(heapPush([1], 2)[0]).toBe(2); - expect(heapPush([1], 0)[0]).toBe(1); - }); - - it("returns an array that its head value is most prior even when an empty array given", () => { - expect(heapPush([], 5)[0]).toBe(5); - }); - - it("mutate the given array", () => { - const array = [3, 1, 2]; - - heapPush(array, 5); - - expect(array).toEqual([5, 3, 2, 1]); - }); -}); - -describe("pop()", () => { - it("returns the most prior value", () => { - expect(heapPop([5, 4, 3, 2, 1])).toBe(5); - }); - - it("returns the most prior value even when an one-element array", () => { - expect(heapPop([1])).toBe(1); - }); - - it("returns undefined even when an empty array given", () => { - expect(heapPop([])).toBe(undefined); - }); - - it("mutates the given array and keeps its head value most prior", () => { - const array = [5, 3, 4, 2, 1]; - - heapPop(array); - - expect(array).toEqual([4, 3, 1, 2]); - }); -}); diff --git a/utilities/Heapify.ts b/utilities/Heapify.ts deleted file mode 100644 index 54e3701..0000000 --- a/utilities/Heapify.ts +++ /dev/null @@ -1,73 +0,0 @@ -export function heapPush( - array: T[], - value: T, - comparator: OrderComparator = compare -): T[] { - array.push(value); - - if (array.length >= 2) { - let target = array.length - 1; - - while (true) { - const parent = Math.floor((target - 1) / 2); - - if (array[parent] === undefined) break; - if (comparator(array[target], array[parent]) <= 0) break; - - [array[target], array[parent]] = [array[parent], array[target]]; - - target = parent; - } - } - - return array; -} - -export function heapPop( - array: T[], - comparator: OrderComparator = compare -): T | undefined { - if (array.length <= 1) return array.pop(); - - const rootValue = array[0]; - array[0] = array.pop()!; - - let target = 0; - - while (true) { - const left = target * 2 + 1; - const right = target * 2 + 2; - let largest = target; - - if ( - array[left] !== undefined && - comparator(array[left], array[largest]) > 0 - ) { - largest = left; - } - - if ( - array[right] !== undefined && - comparator(array[right], array[largest]) > 0 - ) { - largest = right; - } - - if (largest === target) break; - - [array[largest], array[target]] = [array[target], array[largest]]; - - target = largest; - } - - return rootValue; -} - -type OrderComparator = (a: T, b: T) => number; - -export function compare(a: T, b: T): number { - if (a < b) return -1; - if (a > b) return 1; - - return 0; -} diff --git a/utilities/ListNode.test.ts b/utilities/ListNode.test.ts deleted file mode 100644 index bcdb03e..0000000 --- a/utilities/ListNode.test.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { createListNode } from "./ListNode"; - -describe("createListNode", () => { - test("creates ListNode by an array #1", () => { - expect(createListNode([1, 2, 3, 4, 5, 6, 7])).toEqual({ - val: 1, - next: { - val: 2, - next: { - val: 3, - next: { - val: 4, - next: { - val: 5, - next: { - val: 6, - next: { - val: 7, - next: null - } - } - } - } - } - } - }); - }); - - test("creates ListNode by an array #2", () => { - expect(createListNode([1])).toEqual({ - val: 1, - next: null - }); - }); - - test("returns null if the given array has no element", () => { - expect(createListNode([])).toBe(null); - }); -}); diff --git a/utilities/ListNode.ts b/utilities/ListNode.ts deleted file mode 100644 index 02f9fae..0000000 --- a/utilities/ListNode.ts +++ /dev/null @@ -1,20 +0,0 @@ -export interface ListNode { - val: T; - next: ListNode | null; -} - -export function createListNode(array: T[]): ListNode | null { - if (array.length === 0) return null; - - const head: ListNode = { val: array[0], next: null }; - let tail = head; - - for (let i = 1; i < array.length; ++i) { - const node = { val: array[i], next: null }; - - tail.next = node; - tail = node; - } - - return head; -}