Skip to content

Commit 08471e1

Browse files
committed
feat: add solution of Path Sum(112) with javascript.
1 parent 7b8ae19 commit 08471e1

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
| [108][108-question] | [Convert Sorted Array to Binary Search Tree][108-tips] | [Easy][E] | [][108-java] | [][108-js] | [][108-kotlin] |
8181
| [110][110-question] | [Balanced Binary Tree][110-tips] | [Easy][E] | [][110-java] | [][110-js] | [][110-kotlin] |
8282
| [111][111-question] | [Minimum Depth of Binary Tree][111-tips] | [Easy][E] | [][111-java] | [][111-js] | [][111-kotlin] |
83-
| [112][112-question] | [Path Sum][112-tips] | [Easy][E] | [][112-java] | | [][112-kotlin] |
83+
| [112][112-question] | [Path Sum][112-tips] | [Easy][E] | [][112-java] | [][112-js] | [][112-kotlin] |
8484
| [118][118-question] | [Pascal's Triangle][118-tips] | [Easy][E] | [][118-java] | | [][118-kotlin] |
8585
| [119][119-question] | [Pascal's Triangle II][119-tips] | [Easy][E] | [][119-java] | | [][119-kotlin] |
8686
| [121][121-question] | [Best Time to Buy and Sell Stock][121-tips] | [Easy][E] | [][121-java] | | [][121-kotlin] |
@@ -460,6 +460,7 @@ commit信息模板: ``feat: add the solution of `Two Sum`(001) with Java``
460460
[108-js]: ./src/_108/Solution.js
461461
[110-js]: ./src/_110/Solution.js
462462
[111-js]: ./src/_111/Solution.js
463+
[112-js]: ./src/_112/Solution.js
463464
[226-js]: ./src/_226/Solution.js
464465
[561-js]: ./src/_561/Solution.js
465466
[643-js]: ./src/_643/Solution.js

src/_112/Solution.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @param {number} sum
11+
* @return {boolean}
12+
*/
13+
var hasPathSum = function(root, sum) {
14+
if(root == null) {
15+
return false
16+
}
17+
if(root.val === sum && root.left === null && root.right === null) {
18+
return true
19+
}
20+
let sumNext = sum - root.val
21+
return hasPathSum(root.left, sumNext) || hasPathSum(root.right, sumNext)
22+
};

tips/112/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ class Solution {
5858
}
5959
```
6060

61+
```JavaScript
62+
var hasPathSum = function(root, sum) {
63+
if(root == null) {
64+
return false
65+
}
66+
if(root.val === sum && root.left === null && root.right === null) {
67+
return true
68+
}
69+
let sumNext = sum - root.val
70+
return hasPathSum(root.left, sumNext) || hasPathSum(root.right, sumNext)
71+
};
72+
```
6173
## 结语
6274

6375
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]

0 commit comments

Comments
 (0)