## 一.题目描述
Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.
For example:
Given the below binary tree and `sum = 22`,
~~~
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
~~~
return
~~~
[
[5,4,11,2],
[5,8,4,5]
]
~~~
## 二.题目分析
这道题与Path Sum类似,不同的是这道题要求找到所有和等于给定值的路径,必须遍历完所有的路径。在找到满足的路径之后,不能直接返回,而是将其添加到一个`vector<vector<int>>`中。在查找的过程中,每经过一个结点,先使用一个`vector<int>`将该路径中的所有结点记录下来。需要注意的是,在进入每一个结点的时候,先将该结点的值`push`到`vector`中,在退出时将该结点的值`pop`出来,这样就可以避免有时会忘记`pop`结点的值的情况。
该方法的时间复杂度为O(n),空间复杂度为O(logn)。
## 三.示例代码
~~~
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution
{
public:
vector<vector<int>> pathSum(TreeNode* root, int sum)
{
vector<vector<int> > Result;
vector<int> TmpResult;
pathSum(root, sum, Result, TmpResult);
return Result;
}
private:
void pathSum(TreeNode* root, int sum, vector<vector<int> > &Result, vector<int> &TmpResult)
{
if (!root)
return;
if (!root->left && !root->right && root->val == sum)
{
TmpResult.push_back(sum);
Result.push_back(TmpResult);
// pop the leaf node
TmpResult.pop_back();
return;
}
int SumChild = sum - root->val;
TmpResult.push_back(root->val);
pathSum(root->left, SumChild, Result, TmpResult);
pathSum(root->right, SumChild, Result, TmpResult);
TmpResult.pop_back();
}
};
~~~
## 四.小结
与Path Sum一样,用DFS解决。
- 前言
- 2Sum
- 3Sum
- 4Sum
- 3Sum Closest
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- Search in Rotated Sorted Array
- Remove Element
- Merge Sorted Array
- Add Binary
- Valid Palindrome
- Permutation Sequence
- Single Number
- Single Number II
- Gray Code(2016腾讯软件开发笔试题)
- Valid Sudoku
- Rotate Image
- Power of two
- Plus One
- Gas Station
- Set Matrix Zeroes
- Count and Say
- Climbing Stairs(斐波那契数列问题)
- Remove Nth Node From End of List
- Linked List Cycle
- Linked List Cycle 2
- Integer to Roman
- Roman to Integer
- Valid Parentheses
- Reorder List
- Path Sum
- Simplify Path
- Trapping Rain Water
- Path Sum II
- Factorial Trailing Zeroes
- Sudoku Solver
- Isomorphic Strings
- String to Integer (atoi)
- Largest Rectangle in Histogram
- Binary Tree Preorder Traversal
- Evaluate Reverse Polish Notation(逆波兰式的计算)
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Longest Common Prefix
- Recover Binary Search Tree
- Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II
- Binary Tree Zigzag Level Order Traversal
- Sum Root to Leaf Numbers
- Anagrams
- Unique Paths
- Unique Paths II
- Triangle
- Maximum Subarray(最大子串和问题)
- House Robber
- House Robber II
- Happy Number
- Interlaving String
- Minimum Path Sum
- Edit Distance
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock IV
- Decode Ways
- N-Queens
- N-Queens II
- Restore IP Addresses
- Combination Sum
- Combination Sum II
- Combination Sum III
- Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Longest Consecutive Sequence
- Word Search
- Word Search II
- Word Ladder
- Spiral Matrix
- Jump Game
- Jump Game II
- Longest Substring Without Repeating Characters
- First Missing Positive
- Sort Colors
- Search for a Range
- First Bad Version
- Search Insert Position
- Wildcard Matching