Skip to content

Commit 2712a2a

Browse files
authored
Merge pull request DaleStudy#371 from highballplz/main
[highball] week2 solutions
2 parents 45ea72e + 3b9560e commit 2712a2a

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
function TreeNode(val, left, right) {
2+
this.val = val === undefined ? 0 : val;
3+
this.left = left === undefined ? null : left;
4+
this.right = right === undefined ? null : right;
5+
}
6+
7+
const buildTree = function (preorder, inorder) {
8+
if (preorder.length === 0) return null;
9+
10+
const rootNode = new TreeNode(preorder[0]);
11+
const rootValueIndexAtInorder = inorder.indexOf(preorder[0]);
12+
13+
const leftLength = rootValueIndexAtInorder - 0;
14+
const rightLength = inorder.length - 1 - rootValueIndexAtInorder;
15+
16+
const leftPreorder = preorder.slice(1, 1 + leftLength);
17+
const leftInorder = inorder.slice(0, 0 + leftLength);
18+
rootNode.left = buildTree(leftPreorder, leftInorder);
19+
20+
const rightPreorder = preorder.slice(
21+
1 + leftLength,
22+
1 + leftLength + rightLength
23+
);
24+
const rightInorder = inorder.slice(
25+
rootValueIndexAtInorder + 1,
26+
rootValueIndexAtInorder + 1 + rightLength
27+
);
28+
rootNode.right = buildTree(rightPreorder, rightInorder);
29+
30+
return rootNode;
31+
};
32+
33+
function bfsTraversal(root) {
34+
const treeValues = [];
35+
36+
const queue = [];
37+
queue.push(root);
38+
39+
while (queue.length > 0) {
40+
const n = queue.length;
41+
42+
for (let i = 0; i < n; i++) {
43+
const currentNode = queue.shift();
44+
45+
if (currentNode) {
46+
treeValues.push(currentNode.val);
47+
queue.push(currentNode.left);
48+
queue.push(currentNode.right);
49+
} else {
50+
treeValues.push(null);
51+
queue.push(null);
52+
queue.push(null);
53+
}
54+
}
55+
56+
if (queue.every((el) => el === null)) break;
57+
}
58+
59+
return treeValues;
60+
}
61+
62+
// console.log(bfsTraversal(buildTree([3, 9, 20, 15, 7], [9, 3, 15, 20, 7])));
63+
console.log(bfsTraversal(buildTree([1, 2], [2, 1])));

counting-bits/highball.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const countBits = function (n) {
2+
const arr = new Array(n + 1);
3+
arr[0] = 0;
4+
if (n === 0) return arr;
5+
arr[1] = 1;
6+
if (n === 1) return arr;
7+
8+
for (i = 2; i <= n; i++) {
9+
if (i % 2 === 0) arr[i] = arr[i / 2];
10+
else arr[i] = arr[(i - 1) / 2] + 1;
11+
}
12+
13+
return arr;
14+
};

encode-and-decode-strings/highball.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const encode = function (arr) {
2+
const encodedStr = arr.reduce((acc, cur) => acc + "#" + cur);
3+
return encodedStr;
4+
};
5+
6+
const decode = function (str) {
7+
const decodedArr = str.split("#");
8+
return decodedArr;
9+
};

valid-anagram/highball.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const isAnagram = function (s, t) {
2+
const sCharCount = new Array(26).fill(0);
3+
const tCharCount = new Array(26).fill(0);
4+
5+
Array.from(s).forEach((char) => {
6+
const charAsInt = char.charCodeAt(0) - 97;
7+
sCharCount[charAsInt]++;
8+
});
9+
10+
Array.from(t).forEach((char) => {
11+
const charAsInt = char.charCodeAt(0) - 97;
12+
tCharCount[charAsInt]++;
13+
});
14+
15+
return sCharCount.reduce((acc, cur, i) => acc && cur === tCharCount[i], true);
16+
};

0 commit comments

Comments
 (0)