BFS:
struct Node{
TreeNode* p;
int sum;
vector<int> v;
};
class Solution {
public:
vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
if(root == NULL) return *(new vector<vector<int> >);
vector<vector<int> > res;
queue<Node> q;
Node t;
t.p = root;
t.sum = root->val;
t.v.push_back(root->val);
q.push(t);
while(!q.empty()){
t = q.front();
q.pop();
if( t.p->left == NULL && t.p->right == NULL){
if(t.sum == expectNumber){
res.push_back(t.v);
}
continue;
}
if(t.p->left != NULL){
Node t1;
t1.p = t.p->left;
t1.sum = t.sum + t1.p->val;
t1.v = t.v;
t1.v.push_back(t1.p->val);
q.push(t1);
}
if(t.p->right != NULL){
Node t2;
t2.p = t.p->right;
t2.sum = t.sum + t2.p->val;
t2.v = t.v;
t2.v.push_back(t2.p->val);
q.push(t2);
}
}
vector<vector<int> > ans;
int l = res.size()-1;
for(int i=l;i>=0;i--){
ans.push_back(res[i]);
}
return ans;
}
};