Skip to content

Commit ba6f328

Browse files
authored
437 solved. (#62)
1 parent 5392929 commit ba6f328

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ continually updating 😃.
108108
* [235. Lowest Common Ancestor of a Binary Search Tree](src/0235_lowest_common_ancestor_of_a_binary_search_tree/lcaoabst.go)   *`binary tree;`*
109109
* [257. Binary Tree Paths](src/0257_binary_tree_paths/binary_tree_paths.go)   *`binary tree`*
110110
* [404. Sum of Left Leaves](src/0404_sum_of_left_leaves/sum_of_left_leaves.go)   *`binary tree`*
111+
* [437. Path Sum III](src/0437_path_sum_3/path_sum_3.go)   *`binary tree`*
111112

112113
### Binary Search
113114
* [33. Search in Rotated Sorted Array](./src/0033_search_in_rotated_sorted_array/search_in_rotated_sorted_array.go)   *`array;`*  *`binary search`*

src/0437_path_sum_3/path_sum_3.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
437. Path Sum III
3+
https://leetcode.com/problems/path-sum-iii/
4+
5+
You are given a binary tree in which each node contains an integer value.
6+
7+
Find the number of paths that sum to a given value.
8+
9+
The path does not need to start or end at the root or a leaf,
10+
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+
// 2019-01-08
15+
16+
package ps3
17+
18+
// TreeNode Definition for a binary tree node.
19+
type TreeNode struct {
20+
Val int
21+
Left *TreeNode
22+
Right *TreeNode
23+
}
24+
25+
// recursive
26+
// time complexity: O(n), where n is the nodes number of the tree.
27+
// space complexity: O(h), where h is the height of the tree.
28+
func pathSum(root *TreeNode, sum int) int {
29+
if nil == root {
30+
return 0
31+
}
32+
return findPath(root, sum) + pathSum(root.Left, sum) + pathSum(root.Right, sum)
33+
}
34+
35+
func findPath(node *TreeNode, val int) int {
36+
if node == nil {
37+
return 0
38+
}
39+
40+
res := 0
41+
if val == node.Val {
42+
res += 1
43+
}
44+
45+
res += findPath(node.Left, val-node.Val)
46+
res += findPath(node.Right, val-node.Val)
47+
return res
48+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package ps3
2+
3+
import "testing"
4+
5+
func createBinaryTree(nums []int) *TreeNode {
6+
return performCreate(nums, 0)
7+
}
8+
9+
func performCreate(nums []int, index int) *TreeNode {
10+
if index >= len(nums) {
11+
return nil
12+
}
13+
14+
tree := TreeNode{Val: nums[index]}
15+
tree.Left = performCreate(nums, 2*index+1)
16+
tree.Right = performCreate(nums, 2*index+2)
17+
return &tree
18+
}
19+
20+
func TestPathSum(t *testing.T) {
21+
root := createBinaryTree([]int{10, 5, -3, 3, 2, 9, 11, 3, -2, 3, 1})
22+
sum := 8
23+
expected := 3
24+
25+
if res := pathSum(root, sum); res != expected {
26+
t.Errorf("expected %d, got %d", expected, res)
27+
}
28+
}

src/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
|0404|[404. Sum of Left Leaves](0404_sum_of_left_leaves/sum_of_left_leaves.go)|Easy|*`binary tree`*|
9090
|0416|[Partition Equal Subset Sum](./0416_partition_equal_subset_sum/partition_equal_subset_sum.go)|Medium|*`dp;`* *`0-1 knapsack problem`*|
9191
|0435|[Non-overlapping Intervals(dp solution)](./0435_non_overlapping_intervals/dp_solution.go) <br/>[Non-overlapping Intervals(greedy solution)](./0435_non_overlapping_intervals/greedy_solution.go)|Medium|*`dp;`* *`0-1 knapsack problem`*|
92+
|0437|[437. Path Sum III](0437_path_sum_3/path_sum_3.go)|Easy|*`binary tree`*|
9293
|0438|[ Find All Anagrams in a String](./0438_all_anagrams_in_a_string/all_anagrams_in_a_string.go)|Easy|*`sliding window`*|
9394
|0447|[Number of Boomerangs](./0447_number_of_boomerangs/number_of_boomerangs.go)|Easy||
9495
|0454|[4Sum II](./0454_4sum2/4sum2.go)|Medium||

0 commit comments

Comments
 (0)