Update 0437

This commit is contained in:
YDZ
2021-05-04 09:59:35 +08:00
parent 65f4c4f060
commit 99fe55c7df
26 changed files with 687 additions and 564 deletions

View File

@ -16,6 +16,30 @@ type TreeNode = structures.TreeNode
* }
*/
// 解法一 带缓存 dfs
func pathSum(root *TreeNode, targetSum int) int {
prefixSum := make(map[int]int)
prefixSum[0] = 1
return dfs(root, prefixSum, 0, targetSum)
}
func dfs(root *TreeNode, prefixSum map[int]int, cur, sum int) int {
if root == nil {
return 0
}
cur += root.Val
cnt := 0
if v, ok := prefixSum[cur-sum]; ok {
cnt = v
}
prefixSum[cur]++
cnt += dfs(root.Left, prefixSum, cur, sum)
cnt += dfs(root.Right, prefixSum, cur, sum)
prefixSum[cur]--
return cnt
}
// 解法二
func pathSumIII(root *TreeNode, sum int) int {
if root == nil {
return 0
@ -39,25 +63,3 @@ func findPath437(root *TreeNode, sum int) int {
res += findPath437(root.Right, sum-root.Val)
return res
}
func pathSum(root *TreeNode, targetSum int) int {
prefixSum := make(map[int]int)
prefixSum[0] = 1
return dfs(root, prefixSum, 0, targetSum)
}
func dfs(root *TreeNode, prefixSum map[int]int, cur, sum int) int {
if root == nil {
return 0
}
cur += root.Val
cnt := 0
if v, ok := prefixSum[cur-sum]; ok {
cnt = v
}
prefixSum[cur]++
cnt += dfs(root.Left, prefixSum, cur, sum)
cnt += dfs(root.Right, prefixSum, cur, sum)
prefixSum[cur]--
return cnt
}

115
leetcode/0437.Path-Sum-III/README.md Executable file → Normal file
View File

@ -3,32 +3,34 @@
## 题目
You are given a binary tree in which each node contains an integer value.
Given the `root` of a binary tree and an integer `targetSum`, return *the number of paths where the sum of the values along the path equals* `targetSum`.
Find the number of paths that sum to a given value.
The path does not need to start or end at the root or a leaf, but it must go downwards (i.e., traveling only from parent nodes to child nodes).
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).
**Example 1:**
The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.
![https://assets.leetcode.com/uploads/2021/04/09/pathsum3-1-tree.jpg](https://assets.leetcode.com/uploads/2021/04/09/pathsum3-1-tree.jpg)
**Example:**
```
Input: root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8
Output: 3
Explanation: The paths that sum to 8 are shown.
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8
10
/ \
5 -3
/ \ \
3 2 11
/ \ \
3 -2 1
Return 3. The paths that sum to 8 are:
1. 5 -> 3
2. 5 -> 2 -> 1
3. -3 -> 11
```
**Example 2:**
```
Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
Output: 3
```
**Constraints:**
- The number of nodes in the tree is in the range `[0, 1000]`.
- `109 <= Node.val <= 109`
- `1000 <= targetSum <= 1000`
## 题目大意
@ -42,3 +44,76 @@ The tree has no more than 1,000 nodes and the values are in the range -1,000,000
- 注意这一题可能出现负数的情况,节点和为 sum并不一定是最终情况有可能下面还有正数节点和负数节点相加正好为 0那么这也是一种情况。一定要遍历到底。
- 一个点是否为 sum 的起点,有 3 种情况,第一种情况路径包含该 root 节点,如果包含该结点,就在它的左子树和右子树中寻找和为 `sum-root.Val` 的情况。第二种情况路径不包含该 root 节点,那么就需要在它的左子树和右子树中分别寻找和为 sum 的结点。
## 代码
```go
package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
// 解法一 带缓存 dfs
func pathSum(root *TreeNode, targetSum int) int {
prefixSum := make(map[int]int)
prefixSum[0] = 1
return dfs(root, prefixSum, 0, targetSum)
}
func dfs(root *TreeNode, prefixSum map[int]int, cur, sum int) int {
if root == nil {
return 0
}
cur += root.Val
cnt := 0
if v, ok := prefixSum[cur-sum]; ok {
cnt = v
}
prefixSum[cur]++
cnt += dfs(root.Left, prefixSum, cur, sum)
cnt += dfs(root.Right, prefixSum, cur, sum)
prefixSum[cur]--
return cnt
}
// 解法二
func pathSumIII(root *TreeNode, sum int) int {
if root == nil {
return 0
}
res := findPath437(root, sum)
res += pathSumIII(root.Left, sum)
res += pathSumIII(root.Right, sum)
return res
}
// 寻找包含 root 这个结点,且和为 sum 的路径
func findPath437(root *TreeNode, sum int) int {
if root == nil {
return 0
}
res := 0
if root.Val == sum {
res++
}
res += findPath437(root.Left, sum-root.Val)
res += findPath437(root.Right, sum-root.Val)
return res
}
```