您的位置:首页 > 其它

leetcode - Path Sum II

2013-11-06 10:19 351 查看
/**
* Definition for binary tree
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void getResult (TreeNode * node, vector<vector<int>> & rlt, int target, vector<int> & curPath){
target -= node->val;
vector<int> path = curPath;
path.push_back(node->val);
if (node->left==NULL && node->right==NULL && target==0){
rlt.push_back(path);
return;
}
if (node->left!=NULL)
getResult(node->left, rlt, target, path);
if (node->right!=NULL)
getResult(node->right, rlt, target, path);
return;
}
vector<vector<int> > pathSum(TreeNode *root, int sum) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
vector<vector<int> > rlt;
if (root==NULL)
return rlt;
vector<int> path;
getResult(root, rlt, sum, path);
return rlt;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: