Skip to content

Commit 7ecb8cc

Browse files
committed
solve problem Path Sum III
1 parent 2eb6e3b commit 7ecb8cc

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ All solutions will be accepted!
126126
|844|[Backspace String Compare](https://leetcode-cn.com/problems/backspace-string-compare/description/)|[java/py/js](./algorithms/BackspaceStringCompare)|Easy|
127127
|26|[Remove Duplicates From Sorted Array](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/description/)|[java/py/js](./algorithms/RemoveDuplicatesFromSortedArray)|Easy|
128128
|112|[Path Sum](https://leetcode-cn.com/problems/path-sum/description/)|[java/py/js](./algorithms/PathSum)|Easy|
129+
|437|[Path Sum III](https://leetcode-cn.com/problems/path-sum-iii/description/)|[java/py/js](./algorithms/PathSumIII)|Easy|
129130
|263|[Ugly Number](https://leetcode-cn.com/problems/ugly-number/description/)|[java/py/js](./algorithms/UglyNumber)|Easy|
130131
|783|[Minimum Distance Between Bst Nodes](https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/description/)|[java/py/js](./algorithms/MinimumDistanceBetweenBstNodes)|Easy|
131132
|747|[Largest Number At Least Twice Of Others](https://leetcode-cn.com/problems/largest-number-at-least-twice-of-others/description/)|[java/py/js](./algorithms/LargestNumberAtLeastTwiceOfOthers)|Easy|

algorithms/PathSumIII/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Path Sum III
2+
This problem is easy to solve by recursive

algorithms/PathSumIII/Solution.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public int pathSum(TreeNode root, int sum) {
12+
return helper(root, sum, 0, new ArrayList<Integer>());
13+
}
14+
15+
public int helper(TreeNode node, int sum, int curSum, List<Integer> paths) {
16+
if (node == null) return 0;
17+
18+
int res = 0;
19+
curSum += node.val;
20+
paths.add(node.val);
21+
22+
if (curSum == sum) res++;
23+
int tempSum = curSum;
24+
25+
for (int i = 0; i < paths.size() - 1; i++) {
26+
tempSum -= paths.get(i);
27+
if (tempSum == sum) res++;
28+
}
29+
30+
if (node.left != null) res += helper(node.left, sum, curSum, new ArrayList<Integer>(paths));
31+
if (node.right != null) res += helper(node.right, sum, curSum, new ArrayList<Integer>(paths));
32+
return res;
33+
}
34+
}

algorithms/PathSumIII/solution.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 {number}
12+
*/
13+
var pathSum = function(root, sum) {
14+
return helper(root, sum, 0, [])
15+
};
16+
17+
var helper = function(node, sum, curSum, paths) {
18+
if (!node) {
19+
return 0
20+
}
21+
22+
let res = 0
23+
curSum += node.val
24+
paths.push(node.val)
25+
26+
if (curSum === sum) {
27+
res++
28+
}
29+
let tempSum = curSum
30+
31+
for (let i = 0; i < paths.length - 1; i++) {
32+
tempSum -= paths[i]
33+
if (tempSum === sum) {
34+
res++
35+
}
36+
}
37+
38+
if (node.left) {
39+
res += helper(node.left, sum, curSum, paths.slice(0))
40+
}
41+
if (node.right) {
42+
res += helper(node.right, sum, curSum, paths.slice(0))
43+
}
44+
return res
45+
}

algorithms/PathSumIII/solution.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def pathSum(self, root, sum):
10+
"""
11+
:type root: TreeNode
12+
:type sum: int
13+
:rtype: int
14+
"""
15+
return self.helper(root, sum, 0, [])
16+
17+
def helper(self, node, sm, cur_sum, paths):
18+
if not node:
19+
return 0
20+
21+
res = 0
22+
cur_sum += node.val
23+
paths.append(node.val)
24+
25+
if cur_sum == sm:
26+
res += 1
27+
temp_sum = cur_sum
28+
for i in range(len(paths) - 1):
29+
temp_sum -= paths[i]
30+
if temp_sum == sm:
31+
res += 1
32+
33+
34+
if node.left:
35+
res += self.helper(node.left, sm, cur_sum, paths[:])
36+
if node.right:
37+
res += self.helper(node.right, sm, cur_sum, paths[:])
38+
39+
return res

0 commit comments

Comments
 (0)