Skip to content

Commit 34d0395

Browse files
committed
add js solution for leetcode 102 111 and 112
1 parent 670edc9 commit 34d0395

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,8 @@
314314
|115|[Distinct Subsequences](https://leetcode.com/problems/distinct-subsequences/)| |Hard|
315315
|114|[Flatten Binary Tree to Linked List](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/)| |Medium|
316316
|113|[Path Sum II](https://leetcode.com/problems/path-sum-ii/)| |Medium|
317-
|112|[Path Sum](https://leetcode.com/problems/path-sum/)| |Easy|
318-
|111|[Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/)| |Easy|
317+
|112|[Path Sum](https://leetcode.com/problems/path-sum/)| [js](./algorithms/pathSum/pathSum.js) |Easy|
318+
|111|[Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/)| [js](./algorithms/minimumDepthOfBinaryTree/minimumDepthOfBinaryTree.js) |Easy|
319319
|110|[Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/)| [java](./algorithms/balancedBinaryTree/Solution.java) |Easy|
320320
|109|[Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/)| |Medium|
321321
|108|[Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/)| |Medium|
@@ -324,7 +324,7 @@
324324
|105|[Construct Binary Tree from Preorder and Inorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)| |Medium|
325325
|104|[Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/)| [js](./algorithms/maximumDepthOfBinaryTree/maximumDepthOfBinaryTree.js) |Easy|
326326
|103|[Binary Tree Zigzag Level Order Traversal](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/)| |Medium|
327-
|102|[Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/)| |Easy|
327+
|102|[Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/)| [js](./algorithms/binaryTreeLevelOrderTraversal/binaryTreeLevelOrderTraversal.js) |Easy|
328328
|101|[Symmetric Tree](https://leetcode.com/problems/symmetric-tree/)| [java](./algorithms/sysmetricTree/Solution.java) |Easy|
329329
|100|[Same Tree](https://leetcode.com/problems/same-tree/)| [java](./algorithms/sameTree/Solution.java) |Easy|
330330
|99|[Recover Binary Search Tree](https://leetcode.com/problems/recover-binary-search-tree/)| |Hard|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var levelOrder = function(root) {
2+
if (!root) return [];
3+
const res = [];
4+
const q = [[root, 0]];
5+
while (q.length) {
6+
const [n, level] = q.shift();
7+
if (res[level]) {
8+
res[level].push(n.val)
9+
} else {
10+
res[level] = [n.val];
11+
}
12+
if (n.left) q.push([n.left, level + 1]);
13+
if (n.right) q.push([n.right, level + 1]);
14+
}
15+
return res;
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var minDepth = function(root) {
2+
if (!root) return 0;
3+
// 广度优先需要使用一个队列,同时额外再维护一个深度
4+
const q = [[root, 1]];
5+
while (q.length) {
6+
const [n, deepth] = q.shift();
7+
if (!n.left && !n.right) return deepth;
8+
// console.log(n.val, deepth);
9+
if (n.left) q.push([n.left, deepth + 1]);
10+
if (n.right) q.push([n.right, deepth + 1]);
11+
}
12+
};

algorithms/pathSum/pathSum.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var hasPathSum = function(root, sum) {
2+
if(!root) return false;
3+
let res = false;
4+
const dfs = (node, s) => {
5+
// console.log(node.val, s);
6+
// 如果是叶子节点,且当前路径的和为sum,就证明找到一个符合要求的路径
7+
if (!node.left && !node.right && s === sum) {
8+
res = true;
9+
}
10+
if(node.left) dfs(node.left, s + node.left.val);
11+
if(node.right) dfs(node.right, s + node.right.val);
12+
};
13+
dfs(root, root.val);
14+
return res;
15+
};

0 commit comments

Comments
 (0)