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 andsum = 22,
5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1
return
[ [5,4,11,2], [5,8,4,5] ][cpp] view plain copy
class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int sum)
{
vector<vector<int>> ret;
vector<int> tmp;
pathSumone(root, sum, ret, tmp);
}
void pathSumone(TreeNode* root, int sum, vector<vector<int>>& vv, vector<int> tmp)
{
if (root==NULL)
{
return;
}
tmp.push_back(root->val);
if (root->left==NULL && root->right==NULL && sum-root->val==0)
{
vv.push_back(tmp);
}
pathSumone(root->left, sum-root->val, vv, tmp);
pathSumone(root->right, sum-root->val, vv, tmp);
}
};
题意:返回所有路径和为sum的所有路径;