diff --git a/binary-tree-level-order-traversal/yolophg.js b/binary-tree-level-order-traversal/yolophg.js new file mode 100644 index 000000000..38898efe5 --- /dev/null +++ b/binary-tree-level-order-traversal/yolophg.js @@ -0,0 +1,33 @@ +// Time Complexity: O(n) +// Space Complexity: O(n) + +var levelOrder = function (root) { + // if the root is null, return an empty array. + if (root === null) return []; + + const result = []; + const queue = [root]; + + // while there are nodes in the queue, + while (queue.length > 0) { + const levelSize = queue.length; + const currentLevel = []; + + // loop nodes in the current level. + for (let i = 0; i < levelSize; i++) { + // dequeue the front node. + const currentNode = queue.shift(); + // add value to the current level array. + currentLevel.push(currentNode.val); + // enqueue left child if exists. + if (currentNode.left) queue.push(currentNode.left); + // enqueue right child if exists. + if (currentNode.right) queue.push(currentNode.right); + } + + // add the current level array to the result. + result.push(currentLevel); + } + + return result; +}; diff --git a/lowest-common-ancestor-of-a-binary-search-tree/yolophg.js b/lowest-common-ancestor-of-a-binary-search-tree/yolophg.js new file mode 100644 index 000000000..f970b399d --- /dev/null +++ b/lowest-common-ancestor-of-a-binary-search-tree/yolophg.js @@ -0,0 +1,26 @@ +// Time Complexity: O(n) +// Space Complexity: O(1) + +var lowestCommonAncestor = function (root, p, q) { + // start from the root. + let current = root; + + // traverse the tree. + while (current !== null) { + // if both p and q are greater than current node, LCA lies in the right. + if (p.val > current.val && q.val > current.val) { + current = current.right; + } + // if both p and q are smaller than current node, LCA lies in the left. + else if (p.val < current.val && q.val < current.val) { + current = current.left; + } + // if one of p or q is on one side and the other is on the other side, It's LCA. + else { + return current; + } + } + + // if the tree is empty. + return null; +}; diff --git a/remove-nth-node-from-end-of-list/yolophg.js b/remove-nth-node-from-end-of-list/yolophg.js new file mode 100644 index 000000000..5d47325e2 --- /dev/null +++ b/remove-nth-node-from-end-of-list/yolophg.js @@ -0,0 +1,32 @@ +// Time Complexity: O(n) +// Space Complexity: O(1) + +var removeNthFromEnd = function (head, n) { + // calculate the length of the linked list. + let length = 0; + let current = head; + while (current !== null) { + length++; + current = current.next; + } + + // determine the position to remove from the start. + let removeIndex = length - n; + + // if the node to be removed is the head, return the next node. + if (removeIndex === 0) { + return head.next; + } + + // traverse to the node just before the node to be removed. + current = head; + for (let i = 0; i < removeIndex - 1; i++) { + current = current.next; + } + + // remove the nth node from the end. + current.next = current.next.next; + + // return the modified list. + return head; +}; diff --git a/reorder-list/yolophg.js b/reorder-list/yolophg.js new file mode 100644 index 000000000..afbfe5955 --- /dev/null +++ b/reorder-list/yolophg.js @@ -0,0 +1,27 @@ +// Time Complexity: O(n) +// Space Complexity: O(n) + +var reorderList = function (head) { + // push all nodes onto a stack. + let stack = []; + let current = head; + while (current) { + stack.push(current); + current = current.next; + } + + // reorder the list. + let n = stack.length; + current = head; + + for (let i = 0; i < Math.floor(n / 2); i++) { + let next = current.next; + let last = stack.pop(); + + current.next = last; + last.next = next; + current = next; + } + // ensure the last node points to null. + if (current) current.next = null; +}; diff --git a/validate-binary-search-tree/yolophg.js b/validate-binary-search-tree/yolophg.js new file mode 100644 index 000000000..2bd51ab65 --- /dev/null +++ b/validate-binary-search-tree/yolophg.js @@ -0,0 +1,34 @@ +// Time Complexity: O(n) +// Space Complexity: O(n) + +var isValidBST = function (root) { + if (root === null) { + return true; + } + + // initialize a queue for BFS. + let queue = []; + queue.push({ node: root, min: -Infinity, max: Infinity }); + + while (queue.length > 0) { + // dequeue the front one. + let { node, min, max } = queue.shift(); + + // check the BST for the current node. + if (node.val <= min || node.val >= max) { + return false; + } + + // enqueue the left child with updated min and max. + if (node.left !== null) { + queue.push({ node: node.left, min: min, max: node.val }); + } + + // enqueue the right child with updated min and max. + if (node.right !== null) { + queue.push({ node: node.right, min: node.val, max: max }); + } + } + + return true; +};