IBM
Paths from root with a specified sum | C++, Java,
Paths from root with a specified sum | 🧑💻 C++, Java, Python | Binary Tree | Recursion | POTD | GFG
#Paths #root #sum #Java
“Kattamuri Kowshiq”
Link to my WhatsApp Channel: Link to the question: …
source
To see the full content, share this page by clicking one of the buttons below |
Can someone explain whats wrong with my brute force pls 😀
class Solution_ {
public:
void helper(Node *root, int currSum, int targetSum, vector<int> &curr,
vector<vector<int>> &res) {
if (currSum == targetSum) res.push_back(curr);
if (root == nullptr) return;
curr.push_back(root->key);
helper(root->left, currSum + root->key, targetSum, curr, res);
helper(root->right, currSum + root->key, targetSum, curr, res);
curr.pop_back();
}
vector<vector<int>> printPaths(Node *root, int sum) {
vector<vector<int>> res;
vector<int> curr;
helper(root, 0, sum, curr, res);
return res;
}
};