diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/highball.js b/construct-binary-tree-from-preorder-and-inorder-traversal/highball.js new file mode 100644 index 00000000..e267da9f --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/highball.js @@ -0,0 +1,63 @@ +function TreeNode(val, left, right) { + this.val = val === undefined ? 0 : val; + this.left = left === undefined ? null : left; + this.right = right === undefined ? null : right; +} + +const buildTree = function (preorder, inorder) { + if (preorder.length === 0) return null; + + const rootNode = new TreeNode(preorder[0]); + const rootValueIndexAtInorder = inorder.indexOf(preorder[0]); + + const leftLength = rootValueIndexAtInorder - 0; + const rightLength = inorder.length - 1 - rootValueIndexAtInorder; + + const leftPreorder = preorder.slice(1, 1 + leftLength); + const leftInorder = inorder.slice(0, 0 + leftLength); + rootNode.left = buildTree(leftPreorder, leftInorder); + + const rightPreorder = preorder.slice( + 1 + leftLength, + 1 + leftLength + rightLength + ); + const rightInorder = inorder.slice( + rootValueIndexAtInorder + 1, + rootValueIndexAtInorder + 1 + rightLength + ); + rootNode.right = buildTree(rightPreorder, rightInorder); + + return rootNode; +}; + +function bfsTraversal(root) { + const treeValues = []; + + const queue = []; + queue.push(root); + + while (queue.length > 0) { + const n = queue.length; + + for (let i = 0; i < n; i++) { + const currentNode = queue.shift(); + + if (currentNode) { + treeValues.push(currentNode.val); + queue.push(currentNode.left); + queue.push(currentNode.right); + } else { + treeValues.push(null); + queue.push(null); + queue.push(null); + } + } + + if (queue.every((el) => el === null)) break; + } + + return treeValues; +} + +// console.log(bfsTraversal(buildTree([3, 9, 20, 15, 7], [9, 3, 15, 20, 7]))); +console.log(bfsTraversal(buildTree([1, 2], [2, 1]))); diff --git a/counting-bits/highball.js b/counting-bits/highball.js new file mode 100644 index 00000000..c8f316d9 --- /dev/null +++ b/counting-bits/highball.js @@ -0,0 +1,14 @@ +const countBits = function (n) { + const arr = new Array(n + 1); + arr[0] = 0; + if (n === 0) return arr; + arr[1] = 1; + if (n === 1) return arr; + + for (i = 2; i <= n; i++) { + if (i % 2 === 0) arr[i] = arr[i / 2]; + else arr[i] = arr[(i - 1) / 2] + 1; + } + + return arr; +}; diff --git a/encode-and-decode-strings/highball.js b/encode-and-decode-strings/highball.js new file mode 100644 index 00000000..643c6aa9 --- /dev/null +++ b/encode-and-decode-strings/highball.js @@ -0,0 +1,9 @@ +const encode = function (arr) { + const encodedStr = arr.reduce((acc, cur) => acc + "#" + cur); + return encodedStr; +}; + +const decode = function (str) { + const decodedArr = str.split("#"); + return decodedArr; +}; diff --git a/valid-anagram/highball.js b/valid-anagram/highball.js new file mode 100644 index 00000000..0d5fc438 --- /dev/null +++ b/valid-anagram/highball.js @@ -0,0 +1,16 @@ +const isAnagram = function (s, t) { + const sCharCount = new Array(26).fill(0); + const tCharCount = new Array(26).fill(0); + + Array.from(s).forEach((char) => { + const charAsInt = char.charCodeAt(0) - 97; + sCharCount[charAsInt]++; + }); + + Array.from(t).forEach((char) => { + const charAsInt = char.charCodeAt(0) - 97; + tCharCount[charAsInt]++; + }); + + return sCharCount.reduce((acc, cur, i) => acc && cur === tCharCount[i], true); +};