Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb1856f1

Browse files
authored
Create 112-path-sum.js
solved path-sum in JS.
1 parent3cfb9a5 commitb1856f1

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

‎javascript/112-path-sum.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// problem link https://leetcode.com/problems/path-sum/
2+
// time complexity O(n) // whatever the number of nodes are.
3+
4+
varhasPathSum=function(root,targetSum){
5+
6+
constans=[];
7+
functiongoDFS(node,curruntSum){
8+
9+
if(!node)return;
10+
11+
if(!node.left&&!node.right){
12+
ans.push(node.val+curruntSum);
13+
}
14+
15+
goDFS(node.left,curruntSum+node.val);
16+
goDFS(node.right,curruntSum+node.val);
17+
}
18+
goDFS(root,0);
19+
20+
returnans.includes(targetSum);
21+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp