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

Commit aab14cd

Browse files
112. Path Sum
1 parent ade452c commit aab14cd

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

‎src/112. Path Sum.js

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /