From 14ef09a42183f7a52089958e49b6325a2260534c Mon Sep 17 00:00:00 2001 From: sounmind Date: Mon, 10 Jun 2024 20:59:19 -0400 Subject: [PATCH 1/5] solve: binary tree level order traversal --- binary-tree-level-order-traversal/evan.js | 37 +++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 binary-tree-level-order-traversal/evan.js diff --git a/binary-tree-level-order-traversal/evan.js b/binary-tree-level-order-traversal/evan.js new file mode 100644 index 000000000..c233439de --- /dev/null +++ b/binary-tree-level-order-traversal/evan.js @@ -0,0 +1,37 @@ +/** + * @param {TreeNode} root + * @return {number[][]} + */ +var levelOrder = function (root) { + const result = []; + + const dfs = (node, level) => { + if (!node) { + return; + } + + if (!result[level]) { + result[level] = []; + } + + result[level].push(node.val); + + dfs(node.left, level + 1); + dfs(node.right, level + 1); + }; + + dfs(root, 0); + + return result; +}; + +/** + * Time Complexity: O(n) + * - The function visits each node exactly once, making it O(n). + * - Here, n is the number of nodes in the binary tree. + * + * Space Complexity: O(n) + * - The space complexity is determined by the recursion stack. + * - In the worst case, the recursion stack could store all nodes in the binary tree. + * - Thus, the space complexity is O(n). + */ From 05d1725e7cf1ce062f85dddde9a668ffc4606f67 Mon Sep 17 00:00:00 2001 From: sounmind Date: Tue, 11 Jun 2024 21:57:48 -0400 Subject: [PATCH 2/5] solve: reorder list --- reorder-list/evan.js | 77 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 reorder-list/evan.js diff --git a/reorder-list/evan.js b/reorder-list/evan.js new file mode 100644 index 000000000..f4be9eece --- /dev/null +++ b/reorder-list/evan.js @@ -0,0 +1,77 @@ +/** + * Definition for singly-linked list. + * function ListNode(val, next) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + */ +/** + * @param {ListNode} head + * @return {void} Do not return anything, modify head in-place instead. + */ +var reorderList = function (head) { + const middleNode = findMiddleNode(head); + let reversedHalf = reverseList(middleNode.next); + middleNode.next = null; + + let half = head; + + while (reversedHalf) { + const nextForward = half.next; + const nextBackward = reversedHalf.next; + + half.next = reversedHalf; + reversedHalf.next = nextForward; + + reversedHalf = nextBackward; + half = nextForward; + } +}; +/** + * Time Complexity: O(n) + * - Finding the middle node takes O(n). + * - Reversing the second half takes O(n). + * - Merging the two halves takes O(n). + * - Overall, the time complexity is O(n) + O(n) + O(n) = O(n). + * + * Space Complexity: O(1) + * - We only use a constant amount of extra space (pointers), + * so the space complexity is O(1). + */ + +/** + * @param {ListNode} head + * @return {ListNode} + */ +var reverseList = function (head) { + let currentNode = head; + let previousNode = null; + + while (currentNode !== null) { + const nextNode = currentNode.next; + + currentNode.next = previousNode; + previousNode = currentNode; + currentNode = nextNode; + } + + return previousNode; +}; + +/** + * @param {ListNode} head + * @return {ListNode} + */ +var findMiddleNode = function (head) { + if (!head) return null; + + let slow = head; + let fast = head; + + while (fast !== null && fast.next !== null) { + slow = slow.next; + fast = fast.next.next; + } + + return slow; +}; From 5e53d5f40e4eecccc78f7901d93125c6d2805ada Mon Sep 17 00:00:00 2001 From: sounmind Date: Fri, 14 Jun 2024 21:59:27 -0400 Subject: [PATCH 3/5] solve: remove nth node from end of list --- remove-nth-node-from-end-of-list/evan.js | 33 ++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 remove-nth-node-from-end-of-list/evan.js diff --git a/remove-nth-node-from-end-of-list/evan.js b/remove-nth-node-from-end-of-list/evan.js new file mode 100644 index 000000000..aaf33b05e --- /dev/null +++ b/remove-nth-node-from-end-of-list/evan.js @@ -0,0 +1,33 @@ +/** + * @param {ListNode} head + * @param {number} n + * @return {ListNode} + */ +function removeNthFromEnd(head, n) { + let dummy = new ListNode(0); + dummy.next = head; + let first = dummy; + let second = dummy; + + // Move the first pointer n+1 steps ahead + for (let i = 0; i <= n; i++) { + first = first.next; + } + + // Move both pointers until the first pointer reaches the end + // Second pointer will be at the (n+1)th node from the end + while (first !== null) { + first = first.next; + second = second.next; + } + + // Remove the nth node from the end + second.next = second.next.next; + + return dummy.next; +} + +/** + * Time Complexity: O(n) + * Space Complexity: O(1) + */ From cfe38a950c17d7c65598a73263843072a24531ea Mon Sep 17 00:00:00 2001 From: sounmind Date: Fri, 14 Jun 2024 22:24:38 -0400 Subject: [PATCH 4/5] solve: lowest common ancestor of a BST --- .../evan.js | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 lowest-common-ancestor-of-a-binary-search-tree/evan.js diff --git a/lowest-common-ancestor-of-a-binary-search-tree/evan.js b/lowest-common-ancestor-of-a-binary-search-tree/evan.js new file mode 100644 index 000000000..e9ffacddc --- /dev/null +++ b/lowest-common-ancestor-of-a-binary-search-tree/evan.js @@ -0,0 +1,29 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @param {TreeNode} p + * @param {TreeNode} q + * @return {TreeNode} + */ +var lowestCommonAncestor = function (root, p, q) { + if (root.val < p.val && root.val < q.val) { + return lowestCommonAncestor(root.right, p, q); + } + + if (root.val > p.val && root.val > q.val) { + return lowestCommonAncestor(root.left, p, q); + } + + return root; +}; + +/** + * Time Complexity: O(h), where h is the height of the binary search tree. + * Space Complexity: O(h), where h is the height of the binary search tree. + */ From 2c56ca965346bc01c52757e94c192cdd02b1b6e5 Mon Sep 17 00:00:00 2001 From: sounmind Date: Fri, 14 Jun 2024 22:47:09 -0400 Subject: [PATCH 5/5] solve: validate binary search tree --- validate-binary-search-tree/evan.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 validate-binary-search-tree/evan.js diff --git a/validate-binary-search-tree/evan.js b/validate-binary-search-tree/evan.js new file mode 100644 index 000000000..bab2b680f --- /dev/null +++ b/validate-binary-search-tree/evan.js @@ -0,0 +1,28 @@ +/** + * 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) + * } + */ +/** + * @param {TreeNode} root + * @return {boolean} + */ +function isValidBST(root, low = -Infinity, high = Infinity) { + if (root === null) { + return true; + } + + if (root.val <= low || root.val >= high) { + return false; + } + + return ( + // left root should be less than current root + isValidBST(root.left, low, root.val) && + // right root should be greater than current root + isValidBST(root.right, root.val, high) + ); +}