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
| void helper(TreeNode* root, int target, vector<int> s, int currentSum) { currentSum += root->val; s.push_back(root->val); if (!root->left && !root->right) { if (currentSum == target) { for (auto i:s) { cout << i <<" "; } cout << endl; } } if (root->left) { helper(root->left, target, s, currentSum); } if (root->right) { helper(root->right, target, s, currentSum); } s.pop_back(); }
void findPath(TreeNode* root, int target) { if (!root) { return; } vector<int> s; int currentSum = 0; helper(root, target, s, currentSum); }
|