diff --git a/binary-tree-level-order-traversal/nhistory.js b/binary-tree-level-order-traversal/nhistory.js new file mode 100644 index 000000000..846612303 --- /dev/null +++ b/binary-tree-level-order-traversal/nhistory.js @@ -0,0 +1,32 @@ +var levelOrder = function (root) { + // Edge case: If root is null, return [] + if (root === null) return []; + + // Create result and queue + let result = []; + let queue = [root]; + + // Iterate while queue.length is exist + while (queue.length) { + // Create levelArr and levelSize + let levelArr = []; + let levelSize = queue.length; + // Initiate currentNode from queue by using shift method + while (levelSize) { + const currentNode = queue.shift(); + levelArr.push(currentNode.val); + // If currentNode.left is not null, push into the queue + if (currentNode.left) queue.push(currentNode.left); + // If currentNode.right is not null, push into the queue + if (currentNode.right) queue.push(currentNode.right); + + levelSize--; + } + // Push levelArr into result + result.push(levelArr); + } + return result; +}; + +// TC: O(n) +// SC: O(n) diff --git a/lowest-common-ancestor-of-a-binary-search-tree/nhistory.js b/lowest-common-ancestor-of-a-binary-search-tree/nhistory.js new file mode 100644 index 000000000..ee84dc56c --- /dev/null +++ b/lowest-common-ancestor-of-a-binary-search-tree/nhistory.js @@ -0,0 +1,11 @@ +var lowestCommonAncestor = function (root, p, q) { + // Iterate if statement with comparing values + while (root) { + if (root.val < p.val && root.val < q.val) root = root.right; + else if (root.val > p.val && root.val > q.val) root = root.left; + else return root; + } +}; + +// TC: O(n) +// SC: O(1) diff --git a/remove-nth-node-from-end-of-list/nhistory.js b/remove-nth-node-from-end-of-list/nhistory.js new file mode 100644 index 000000000..5d37e2869 --- /dev/null +++ b/remove-nth-node-from-end-of-list/nhistory.js @@ -0,0 +1,33 @@ +var removeNthFromEnd = function (head, n) { + // Edge case: If the list is empty + if (!head) return null; + + // Create a dummy node that points to the head + let dummy = new ListNode(0); + dummy.next = head; + let length = 0, + curr = head; + + // Calculate the length of the list + while (curr) { + length++; + curr = curr.next; + } + + // Find the length-n node from the beginning + length = length - n; + curr = dummy; + while (length > 0) { + length--; + curr = curr.next; + } + + // Skip the desired node + curr.next = curr.next.next; + + // Return the head, which may be a new head if we removed the first node + return dummy.next; +}; + +// TC: O(n) +// SC: O(1) diff --git a/reorder-list/nhistory.js b/reorder-list/nhistory.js new file mode 100644 index 000000000..33267ca6c --- /dev/null +++ b/reorder-list/nhistory.js @@ -0,0 +1,68 @@ +// First option : change node value +var reorderList = function (head) { + if (!head) return; + // Create new arr to return result and current head + let arr = []; + let curr = head; + // Make list array that has every values from linked list + let list = []; + while (curr) { + list.push(curr.val); + curr = curr.next; + } + // Iterate list array if i%2 === 0 then curr.val = list[Math.floor(i / 2)] + // If i%2 !== 0 them curr.val = list[list.length - Math.floor((i + 1) / 2)] + curr = head; + for (let i = 0; i < list.length; i++) { + if (i % 2 === 0) { + curr.val = list[Math.floor(i / 2)]; + } else { + curr.val = list[list.length - Math.floor((i + 1) / 2)]; + } + curr = curr.next; + } +}; + +// TC: O(n) +// SC: O(n) + +// Second option : move node without changing values +var reorderList = function (head) { + // Edge case + if (!head) return; + + // Find mid node from linked list + let slow = head, + fast = head; + while (fast && fast.next) { + slow = slow.next; + fast = fast.next.next; + } + + // Reverse second half list + let prev = null, + curr = slow, + temp; + while (curr) { + temp = curr.next; + curr.next = prev; + prev = curr; + curr = temp; + } + + // Modify linked list as followed instruction + let first = head, + second = prev; + while (second.next) { + temp = first.next; + first.next = second; + first = temp; + + temp = second.next; + second.next = first; + second = temp; + } +}; + +// TC: O(n) +// SC: O(1) diff --git a/validate-binary-search-tree/nhistory.js b/validate-binary-search-tree/nhistory.js new file mode 100644 index 000000000..40ac51b4f --- /dev/null +++ b/validate-binary-search-tree/nhistory.js @@ -0,0 +1,13 @@ +var isValidBST = function (root) { + return validate(root, -Infinity, Infinity); +}; + +function validate(node, min, max) { + if (!node) return true; // An empty tree is a valid BST + if (node.val <= min || node.val >= max) return false; // Current node's value must be between min and max + + // Recursively validate the left and right subtree + return ( + validate(node.left, min, node.val) && validate(node.right, node.val, max) + ); +}