From 028d1b83da17a31178797c3f37337a017c28724e Mon Sep 17 00:00:00 2001 From: Stackingrule <38368554+Stackingrule@users.noreply.github.com> Date: Tue, 22 Dec 2020 15:05:30 +0800 Subject: [PATCH] feat:add Solution.cpp for 0113. Path Sum II --- .../0100-0199/0113.Path Sum II/Solution.cpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 solution/0100-0199/0113.Path Sum II/Solution.cpp diff --git a/solution/0100-0199/0113.Path Sum II/Solution.cpp b/solution/0100-0199/0113.Path Sum II/Solution.cpp new file mode 100644 index 0000000000000..402c7f26ea665 --- /dev/null +++ b/solution/0100-0199/0113.Path Sum II/Solution.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + vector> pathSum(TreeNode* root, int sum) { + vector> res; + vector out; + helper(root, sum, out, res); + return res; + } + +private: + void helper(TreeNode *node, int sum, vector &out, vector> &res) { + if (!node) return; + out.push_back(node->val); + if (sum == node->val && !node->left && !node->right) { + res.push_back(out); + } + helper(node->left, sum - node->val, out, res); + helper(node->right, sum - node->val, out, res); + out.pop_back(); + } +}; \ No newline at end of file