-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #371 from highballplz/main
[highball] week2 solutions
- Loading branch information
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
construct-binary-tree-from-preorder-and-inorder-traversal/highball.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; |