Files
2020-08-07 17:06:53 +08:00

45 lines
1.8 KiB
Markdown
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [437. Path Sum III](https://leetcode.com/problems/path-sum-iii/)
## 题目
You are given a binary tree in which each node contains an integer value.
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 (traveling only from parent nodes to child nodes).
The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.
**Example:**
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
## 题目大意
给定一个二叉树它的每个结点都存放着一个整数值。找出路径和等于给定数值的路径总数。路径不需要从根节点开始也不需要在叶子节点结束但是路径方向必须是向下的只能从父节点到子节点。二叉树不超过1000个节点且节点数值范围是 [-1000000,1000000] 的整数。
## 解题思路
- 这一题是第 112 题 Path Sum 和第 113 题 Path Sum II 的加强版,这一题要求求出任意一条路径的和为 sum起点不一定是根节点可以是任意节点开始。
- 注意这一题可能出现负数的情况,节点和为 sum并不一定是最终情况有可能下面还有正数节点和负数节点相加正好为 0那么这也是一种情况。一定要遍历到底。
- 一个点是否为 sum 的起点,有 3 种情况,第一种情况路径包含该 root 节点,如果包含该结点,就在它的左子树和右子树中寻找和为 `sum-root.Val` 的情况。第二种情况路径不包含该 root 节点,那么就需要在它的左子树和右子树中分别寻找和为 sum 的结点。