0%

剑指 Offer 34. 二叉树中和为某一值的路径

剑指 Offer 34. 二叉树中和为某一值的路径

BackTrack
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int target) {
vector<int> tmp;
helper(root, target, 0, tmp);
return ret;
}
private:
vector<vector<int>> ret;
void helper(TreeNode* root, const int& target, int cur, vector<int>& tmp)
{
if(!root)
return;
cur += root->val;
tmp.push_back(root->val);
if(cur == target && !root->left && !root->right)
{
ret.push_back(tmp);
}
helper(root->right, target, cur, tmp);
helper(root->left, target, cur, tmp);
tmp.pop_back();
}
};