Skip to content

Commit 4ea8b5c

Browse files
committed
week12: add js solution
1 parent 08bb562 commit 4ea8b5c

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@
137137
|380|[Insert GetRandom O(1)](https://leetcode.com/problems/insert--getrandom-o1/) | |Hard|
138138
|377|[Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/) | |Medium|
139139
|376|[Wiggle Subsequence](https://leetcode.com/problems/wiggle-subsequence/) | |Medium|
140+
|374|[Guess Number Higher Or Lower](https://leetcode.com/problems/guess-number-higher-or-lower/) | [js](./algorithms/guessNumberHigherOrLower/Solution.js) |Easy|
140141
|371|[Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/description/) | |Easy|
141142
|367|[Valid Perfect Square](https://leetcode.com/problems/valid-perfect-square/description/) | |Easy|
142143
|357|[Count Numbers with Unique Digits](https://leetcode.com/problems/count-numbers-with-unique-digits/) | |Medium|
@@ -215,7 +216,7 @@
215216
|229|[Majority Element II](https://leetcode.com/problems/majority-element-ii/) | |Medium|
216217
|228|[Summary Ranges](https://leetcode.com/problems/summary-ranges/)| |Easy|
217218
|227|[Basic Calculator II](https://leetcode.com/problems/basic-calculator-ii/)| |Medium|
218-
|226|[Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/)| [java](./algorithms/invertBinaryTree/Solution.java) |Easy|
219+
|226|[Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/)| [java](./algorithms/invertBinaryTree/Solution.java), [js](./algorithms/invertBinaryTree/Solution.js) |Easy|
219220
|225|[Implement Stack using Queues](https://leetcode.com/problems/implement-stack-using-queues/)| |Medium|
220221
|224|[Basic Calculator](https://leetcode.com/problems/basic-calculator/)| |Medium|
221222
|223|[Rectangle Area](https://leetcode.com/problems/rectangle-area/)| |Easy|
@@ -335,7 +336,7 @@
335336
|103|[Binary Tree Zigzag Level Order Traversal](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/)| |Medium|
336337
|102|[Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/)| [js](./algorithms/binaryTreeLevelOrderTraversal/binaryTreeLevelOrderTraversal.js) |Easy|
337338
|101|[Symmetric Tree](https://leetcode.com/problems/symmetric-tree/)| [java](./algorithms/sysmetricTree/Solution.java) |Easy|
338-
|100|[Same Tree](https://leetcode.com/problems/same-tree/)| [java](./algorithms/sameTree/Solution.java) |Easy|
339+
|100|[Same Tree](https://leetcode.com/problems/same-tree/)| [java](./algorithms/sameTree/Solution.java), [js](./algorithms/sameTree/Solution.js) |Easy|
339340
|99|[Recover Binary Search Tree](https://leetcode.com/problems/recover-binary-search-tree/)| |Hard|
340341
|98|[Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/)| |Medium|
341342
|97|[Interleaving String](https://leetcode.com/problems/interleaving-string/)| |Hard|
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var invertTree = function(root) {
2+
if (!root) return null;
3+
4+
return {
5+
val: root.val,
6+
left: invertTree(root.right),
7+
right: invertTree(root.left)
8+
};
9+
};

algorithms/sameTree/Solution.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var isSameTree = function(p, q) {
2+
if (!p && !q) return true
3+
if (p && q && p.val === q.val &&
4+
isSameTree(p.left, q.left) &&
5+
isSameTree(p.right, q.right)
6+
) {
7+
return true;
8+
}
9+
return false;
10+
};

guessNumberHigherOrLower/Solution.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var guessNumber = function(n) {
2+
let low = 1, high = n;
3+
while (low <= high) {
4+
const mid = Math.floor((high + low) / 2);
5+
const res = guess(mid);
6+
if (res === -1) {
7+
high = mid - 1;
8+
} else if (res === 1) {
9+
low = mid + 1;
10+
} else {
11+
return mid;
12+
}
13+
}
14+
};

0 commit comments

Comments
 (0)