题目描述
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] ]
class Solution {
public:
vector<vector<int> > pathSum(TreeNode *root, int sum) {
vector<vector<int> > vv;
vector<int> v;
pathSum_Aux(root,sum,v,vv);
return vv;
}
void pathSum_Aux(TreeNode* root,int sum,vector<int> v,vector<vector<int>>& vv){
if(root == NULL)
return;
v.push_back(root->val);
if(root->left == NULL && root->right == NULL && sum-root->val == 0){
vv.push_back(v);
}
pathSum_Aux(root->left,sum-root->val,v,vv);
pathSum_Aux(root->right,sum-root->val,v,vv);
}
};