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]
]
思路:使用深度优先遍历,需要
按照深度优先的遍历方式遍历,
Code(c++):非递归算法:
/**
* Definition for a binary tree node.
* 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> > res;
if(root==NULL) return res;
vector<int> nums;
map<TreeNode *, int> visited;
stack<TreeNode *> stk;
TreeNode* p = root;
nums.push_back(p->val);
stk.push(p);
visited[p] = 0;
while(!stk.empty()){
p = stk.top();
while(p->left && visited[p] == 0){
//this is very important to break this while
if(visited[p->left]==1) break;
p = p->left;
nums.push_back(p->val);
stk.push(p);
visited[p] = 0;
}
if(!stk.empty()){
p = stk.top();
if(!p->left && !p->right && sumVector(nums) == sum){
res.push_back(nums);
}
if( p->right && (visited.find(p->right) == visited.end() || visited[p->right] == 0)){
p = p->right;
stk.push(p);
nums.push_back(p->val);
visited[p] = 0;
continue;
}
visited[p] = 1;
stk.pop();
nums.resize(nums.size()-1);
}
}
return res;
}
int sumVector(vector<int> & nums){
int n = nums.size();
if( n == 0 ) return 0;
int sum = 0;
for(int i = 0; i < n; i++) {
sum += nums[i];
}
return sum;
}
};
递归方法:
/**
* Definition for a binary tree node.
* 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> nums;
getPath(result,nums,sum,0,root);
return result;
}
void getPath(vector<vector<int> > &res, vector<int> &nums, int target, int sum, TreeNode *root) {
if(root==NULL) return;
sum += root->val;
nums.push_back(root->val);
if(sum == target && !root->left && !root->right){
res.push_back(nums);
}
getPath(res, nums, target, sum, root->left);
getPath(res, nums, target, sum, root->right);
nums.pop_back();
sum -= root->val;
}
};
原文地址:http://blog.csdn.net/dream_angel_z/article/details/46473191