Path Sum II

Desicription

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,

1
2
3
4
5
6
7
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1

return

1
2
3
4
[
[5,4,11,2],
[5,8,4,5]
]

Solution

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
/**
* 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 {
private:
vector<vector<int>> res;
void searchTree(TreeNode* root, int sum, const int& target, vector<int> vec) {
if(root == NULL)
return ;
vec.push_back(root->val);
if(root->left == NULL && root->right == NULL)
if(sum + root->val == target)
res.push_back(vec);
searchTree(root->left, sum + root->val, target, vec);
searchTree(root->right, sum + root->val, target, vec);
}
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
if(root == NULL)
return res;
searchTree(root, 0, sum, vector<int>());
return res;
}
};