Skip to content

Commit 4899abe

Browse files
[LEET-437] add 437
1 parent 34da7e7 commit 4899abe

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

leetcode-algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
|442|[Find All Duplicates in an Array](https://leetcode.com/problems/find-all-duplicates-in-an-array/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/FindAllDuplicatesinanArray.java)| O(n)|O(1) | Medium| Array
4141
|441|[Arranging Coins](https://leetcode.com/problems/arrange-coins/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ArrangingCoins.java)| O(n)|O(1) | Easy|
4242
|438|[Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/FindAllAnagramsinaString.java)| O(n)|O(1) | Easy|
43+
|437|[Path Sum III](https://leetcode.com/problems/path-sum-iii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/PathSumIII.java) | O(n^2) |O(n) | Easy| DFS, recursion
4344
|436|[Find Right Interval](https://leetcode.com/problems/find-right-interval/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/FindRightInterval.java) | O(nlogn) |O(n) | Medium| Binary Search
4445
|435|[Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/NonOverlappingIntervals.java) | O(nlogn) |O(1) | Medium| Greedy
4546
|434|[Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/NumberofSegmentsinaString.java)| O(n)|O(1) | Easy|
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.stevesun.solutions;
2+
3+
import com.stevesun.common.classes.TreeNode;
4+
5+
/**
6+
* You are given a binary tree in which each node contains an integer value.
7+
8+
Find the number of paths that sum to a given value.
9+
10+
The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).
11+
12+
The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.
13+
14+
Example:
15+
16+
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8
17+
18+
10
19+
/ \
20+
5 -3
21+
/ \ \
22+
3 2 11
23+
/ \ \
24+
3 -2 1
25+
26+
Return 3. The paths that sum to 8 are:
27+
28+
1. 5 -> 3
29+
2. 5 -> 2 -> 1
30+
3. -3 -> 11
31+
*/
32+
public class PathSumIII {
33+
34+
public int pathSum(TreeNode root, int sum) {
35+
if (root == null) return 0;
36+
return pathSumFrom(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum);
37+
}
38+
39+
private int pathSumFrom(TreeNode root, int sum) {
40+
if (root == null) return 0;
41+
return (root.val == sum ? 1 : 0) + pathSumFrom(root.left, sum - root.val) + pathSumFrom(root.right, sum - root.val);
42+
}
43+
44+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy