id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
112
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <code>true</code> if the tree has a <strong>root-to-leaf</strong> path such that adding up all the values along the path equals <code>targetSum</code>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 <strong>Output:</strong> true <strong>Explanation:</strong> The root-to-leaf path with the target sum is shown. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> false <strong>Explanation:</strong> There two root-to-leaf paths in the tree: (1 --&gt; 2): The sum is 3. (1 --&gt; 3): The sum is 4. There is no root-to-leaf path with sum = 5. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [], targetSum = 0 <strong>Output:</strong> false <strong>Explanation:</strong> Since the tree is empty, there are no root-to-leaf paths. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool hasPathSum(TreeNode* root, int targetSum) {\n if (!root) {\n return false;\n }\n\n // if (root->val > targetSum) {\n // return false;\n // } else if (root->val == targetSum) {\n // if (!(root->left) && !(root->right)) {return true;} \n // else {return false;}\n // }\n if (!(root->left) && !(root->right)) {\n return root->val == targetSum;\n }\n\n return hasPathSum(root->left, targetSum-root->val) || hasPathSum(root->right, targetSum-root->val);\n }\n};", "memory": "20000" }
112
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <code>true</code> if the tree has a <strong>root-to-leaf</strong> path such that adding up all the values along the path equals <code>targetSum</code>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 <strong>Output:</strong> true <strong>Explanation:</strong> The root-to-leaf path with the target sum is shown. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> false <strong>Explanation:</strong> There two root-to-leaf paths in the tree: (1 --&gt; 2): The sum is 3. (1 --&gt; 3): The sum is 4. There is no root-to-leaf path with sum = 5. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [], targetSum = 0 <strong>Output:</strong> false <strong>Explanation:</strong> Since the tree is empty, there are no root-to-leaf paths. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool ans = false;\n void solve (TreeNode *root, int targetsum){\n if (!root)\n return;\n if (!root->left && !root->right && root->val == targetsum)\n {\n ans = true;\n return;\n }\n\n solve (root->left, targetsum - root->val);\n solve (root->right, targetsum - root->val);\n }\n\n bool hasPathSum(TreeNode* root, int targetSum) {\n solve (root, targetSum);\n return ans;\n }\n};", "memory": "20100" }
112
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <code>true</code> if the tree has a <strong>root-to-leaf</strong> path such that adding up all the values along the path equals <code>targetSum</code>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 <strong>Output:</strong> true <strong>Explanation:</strong> The root-to-leaf path with the target sum is shown. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> false <strong>Explanation:</strong> There two root-to-leaf paths in the tree: (1 --&gt; 2): The sum is 3. (1 --&gt; 3): The sum is 4. There is no root-to-leaf path with sum = 5. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [], targetSum = 0 <strong>Output:</strong> false <strong>Explanation:</strong> Since the tree is empty, there are no root-to-leaf paths. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool hasPathSum(TreeNode* root, int targetSum) {\n \n if(root==NULL )\n return false;\n // if(root==NULL && targetSum==0)\n // return true;\n targetSum-=root->val;\n if((root->left==NULL && root->right==NULL)&& targetSum==0)\n return true;\n if((root->left==NULL && root->right==NULL)&& targetSum!=0)\n return false;\n // if(targetSum<0)\n // return false;\n\n bool l=hasPathSum(root->left, targetSum);\n bool r=hasPathSum(root->right, targetSum);\n return l or r;\n\n }\n};", "memory": "20100" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n if (!root)\n return {};\n \n if (root->val == targetSum && !root->left && !root->right)\n return {{root->val}};\n \n deque<TreeNode*> tree_stack;\n\n TreeNode * current = root;\n int current_sum = current->val;\n bool going_back = false;\n vector<vector<int>> result;\n\n while (true)\n {\n if (current->left)\n {\n tree_stack.push_back(current);\n current = current->left;\n current_sum += current->val;\n going_back = false;\n }\n else\n if (current->right)\n {\n tree_stack.push_back(current);\n current = current->right;\n current_sum += current->val; \n going_back = false;\n }\n else\n {\n if (tree_stack.empty())\n {\n return result;\n }\n else\n if (current_sum == targetSum && !current->left && !current->right && !going_back)\n {\n vector<int> current_vec;\n for (auto item : tree_stack)\n {\n current_vec.push_back(item->val);\n }\n current_vec.push_back(current->val);\n result.push_back(current_vec);\n }\n\n current_sum -= current->val;\n TreeNode * parent = tree_stack.back();\n tree_stack.pop_back();\n going_back = true;\n\n if (current == parent->left)\n {\n delete current;\n current = parent;\n current->left = nullptr;\n }\n else\n if (current == parent->right)\n {\n delete current;\n current = parent;\n current->right = nullptr;\n }\n }\n }\n }\n};", "memory": "14650" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void dfs(TreeNode* root, int sum, int currSum, vector<int>& curr, vector<vector<int>>& ans) {\n if (!root) return;\n\n curr.push_back(root->val);\n currSum += root->val;\n\n if (!root->left && !root->right && currSum == sum) {\n ans.push_back(curr);\n }\n\n dfs(root->left, sum, currSum, curr, ans);\n dfs(root->right, sum,currSum, curr, ans);\n\n curr.pop_back();\n }\n\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>> ans;\n vector<int> curr;\n dfs(root, targetSum, 0, curr, ans);\n return ans;\n }\n};", "memory": "14650" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n void solve(TreeNode* root, int targetSum,vector<vector<int>> &ans,vector<int> &temp)\n {\n if(root == NULL)\n {\n return;\n }\n temp.push_back(root->val);\n if(root->left==NULL && root->right==NULL)\n {\n if(targetSum == root->val)\n {\n ans.push_back(temp);\n temp.pop_back();\n return;\n }\n }\n solve(root->left,targetSum - root->val,ans,temp);\n solve(root->right,targetSum - root->val,ans,temp);\n temp.pop_back();\n }\n\n\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n\n vector<vector<int>> ans;\n vector<int> temp;\n solve(root,targetSum,ans,temp);\n return ans;\n \n }\n};", "memory": "14950" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void sum(vector<vector<int>>&res,vector<int>& v,int s,int target,TreeNode* root){\n \n if(root==nullptr)\n return ;\n \n v.push_back(root->val);\n\n if((root->left==nullptr) and (root->right==nullptr)){\n if(target==s+root->val){\n res.push_back(v);\n }\n }\n \n sum(res,v,s+root->val,target,root->left);\n sum(res,v,s+root->val,target,root->right);\n\n v.pop_back();\n }\n\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>>res;\n vector<int>v;\n sum(res,v,0,targetSum,root);\n return res;\n }\n};", "memory": "14950" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\n public:\n vector<vector<int>> pathSum(TreeNode* root, int sum) {\n vector<vector<int>> ans;\n dfs(root, sum, {}, ans);\n return ans;\n }\n\n private:\n void dfs(TreeNode* root, int sum, vector<int>&& path,\n vector<vector<int>>& ans) {\n if (root == nullptr)\n return;\n if (root->val == sum && root->left == nullptr && root->right == nullptr) {\n path.push_back(root->val);\n ans.push_back(path);\n path.pop_back();\n return;\n }\n\n path.push_back(root->val);\n dfs(root->left, sum - root->val, std::move(path), ans);\n dfs(root->right, sum - root->val, std::move(path), ans);\n path.pop_back();\n }\n};", "memory": "15250" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>>ans;\n vector<int>part;\n int sum = 0;\n if(root==nullptr) return ans;\n TreeNode* last = root;\n TreeNode* parent = nullptr;\n stack<TreeNode*> stk;\n stk.push(root);\n while(!stk.empty()){\n TreeNode* current = stk.top();\n stk.pop();\n part.push_back(current->val);\n sum += current->val;\n if(current->left!=nullptr && current->left!=last && current->right!=last){\n parent = current;\n stk.push(current);\n stk.push(current->left);\n }\n else if(current->right!=nullptr && current->right!=last){\n parent = current;\n stk.push(current);\n stk.push(current->right);\n }\n else{\n if(current->left == nullptr && current->right == nullptr){\n if(sum == targetSum){\n ans.push_back(part);\n }\n }\n part.pop_back();\n if(!part.empty())part.pop_back();\n sum -= current->val;\n if(!stk.empty()) sum -= stk.top()->val;\n }\n last = current;\n\n }\n return ans;\n }\n};", "memory": "15250" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>>ans;\n vector<int>part;\n int sum = 0;\n if(root==nullptr) return ans;\n TreeNode* last = root;\n stack<TreeNode*> stk;\n stk.push(root);\n while(!stk.empty()){\n TreeNode* current = stk.top();\n stk.pop();\n part.push_back(current->val);\n sum += current->val;\n if(current->left!=nullptr && current->left!=last && current->right!=last){\n stk.push(current);\n stk.push(current->left);\n }\n else if(current->right!=nullptr && current->right!=last){\n stk.push(current);\n stk.push(current->right);\n }\n else{\n if(current->left == nullptr && current->right == nullptr){\n if(sum == targetSum){\n ans.push_back(part);\n }\n }\n part.pop_back();\n part.pop_back();\n sum -= current->val;\n if(!stk.empty()) sum -= stk.top()->val;\n }\n last = current;\n\n }\n return ans;\n }\n};", "memory": "15550" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void f(TreeNode* root, int sum, vector<int> &curr, vector<vector<int>> &ans) {\n if (!root) return;\n curr.push_back(root->val);\n if (root->val == sum && !root->left && !root->right) {\n ans.push_back(curr);\n }\n f(root->left, sum - root->val, curr, ans);\n f(root->right, sum -root->val, curr, ans);\n curr.pop_back();\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<int> curr;\n vector<vector<int>> ans;\n f(root, targetSum, curr, ans);\n return ans;\n }\n};", "memory": "15550" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n void helper(TreeNode* root, int targetSum, vector<int>& seq, vector<vector<int>>& res){\n\n if(!root) return;\n //vector<int> seq;\n \n seq.push_back(root->val);\n targetSum -= root->val;\n\n if(!root->left && !root->right && targetSum == 0){\n res.push_back(seq);\n }\n\n helper(root->left, targetSum, seq, res);\n helper(root->right, targetSum, seq, res);\n\n seq.pop_back();\n\n }\n\n\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n\n vector<vector<int>> res;\n vector<int> seq;\n helper(root, targetSum, seq, res);\n return res;\n \n }\n};", "memory": "15850" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n void dfs(TreeNode* root, int targetSum, int currentSum, vector<int>&solution, vector<vector<int>>& res){\n if(root == nullptr){\n return;\n }\n \n currentSum += root -> val;\n solution.push_back(root -> val);\n\n // Check if we're at a leaf node and if the current sum matches the target sum\n if (root->left == nullptr && root->right == nullptr && currentSum == targetSum) {\n res.push_back(solution);\n }\n\n dfs(root -> left, targetSum, currentSum, solution, res);\n dfs(root -> right, targetSum, currentSum, solution, res);\n currentSum -= root -> val;\n solution.pop_back();\n }\n\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>> res;\n vector<int> solution;\n\n int currentSum = 0;\n dfs(root, targetSum, currentSum, solution, res);\n\n return res;\n }\n};", "memory": "15850" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n public:\n vector<vector<int>> pathSum(TreeNode* root, int sum) {\n vector<vector<int>> ans;\n dfs(root, sum, {}, ans);\n return ans;\n }\n\n private:\n void dfs(TreeNode* root, int sum, vector<int>&& path,\n vector<vector<int>>& ans) {\n if (root == nullptr)\n return;\n if (root->val == sum && root->left == nullptr && root->right == nullptr) {\n path.push_back(root->val);\n ans.push_back(path);\n path.pop_back();\n return;\n }\n\n path.push_back(root->val);\n dfs(root->left, sum - root->val, std::move(path), ans);\n dfs(root->right, sum - root->val, std::move(path), ans);\n path.pop_back();\n }\n};", "memory": "16150" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n vector<vector<int>> ans;\n private: \n void findPathSum(TreeNode* root, int targetSum, vector<int>& path) {\n if( root == NULL) {\n return;\n }\n path.push_back(root->val);\n targetSum = targetSum - root->val;\n findPathSum(root->left, targetSum, path);\n findPathSum(root->right, targetSum, path);\n if(root->left == NULL && root->right == NULL && targetSum == 0) {\n ans.push_back(path);\n } \n path.pop_back();\n }\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n \n vector<int> temp;\n findPathSum(root, targetSum, temp); \n return ans;\n }\n};", "memory": "16150" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nprivate:\n void pathSum(TreeNode* node, int targetSum, std::vector<std::vector<int>>& pathLists) {\n if (!node) return;\n\n targetSum -= node->val;\n pathLists.back().push_back(node->val);\n\n if (!node->left && !node->right) {\n if (!targetSum)\n pathLists.push_back(pathLists.back());\n } else {\n pathSum(node->left, targetSum, pathLists);\n pathSum(node->right, targetSum, pathLists);\n }\n\n pathLists.back().pop_back();\n }\n\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n std::vector<std::vector<int>> res;\n res.push_back({});\n pathSum(root, targetSum, res);\n res.pop_back();\n return res;\n }\n};\n", "memory": "16450" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n vector<vector<int>>ans;\n\n void solve(TreeNode *root, int target, vector<int>&op){\n if(!root){\n return ;\n }\n\n op.push_back(root->val);\n\n if(root->left==NULL && root->right==NULL && target==root->val){\n ans.push_back(op);\n }\n\n solve(root->left, target- root->val, op);\n solve(root->right, target - root->val, op);\n\n op.pop_back();\n }\n\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n if(!root) return {};\n vector<int>op;\n vector<vector<int>>sol;\n solve(root, targetSum ,op);\n for(auto a:ans) sol.push_back(a);\n return ans;\n }\n};", "memory": "16450" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n \npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n dfs(root, targetSum, 0);\n return result;\n }\n\n vector<vector<int>> result;\n vector<int> path;\n int currPathSum = 0;\n void dfs(TreeNode* node, int targetSum, int currPathSum) {\n if (!node) return;\n\n currPathSum += node->val;\n path.push_back(node->val);\n if (!node->left && !node->right) {\n if (currPathSum == targetSum) {\n vector<int> findPath(path.begin(), path.end());\n result.push_back(findPath);\n \n }\n path.pop_back();\n return;\n }\n\n dfs(node->left, targetSum, currPathSum);\n path.pop_back();\n \n \n path.push_back(node->val);\n dfs(node->right, targetSum, currPathSum);\n path.pop_back();\n }\n};", "memory": "16750" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void recur(TreeNode* root, int &targetSum, int &movingSum, vector<vector<int>> &ret, vector<int> &tmp){\n if(root->left==NULL && root->right==NULL){\n if(movingSum==targetSum){\n vector<int> newData(tmp.begin(), tmp.end());\n ret.push_back(newData);\n }\n return;\n }\n if(root->left!=NULL){\n movingSum+=root->left->val;\n tmp.push_back(root->left->val);\n recur(root->left, targetSum, movingSum, ret, tmp);\n movingSum-=root->left->val;\n tmp.pop_back();\n }\n if(root->right!=NULL){\n movingSum+=root->right->val;\n tmp.push_back(root->right->val);\n recur(root->right, targetSum, movingSum, ret, tmp);\n movingSum-=root->right->val;\n tmp.pop_back();\n }\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n //do dfs, upon reaching the leaf node, check if the moving sum is target sum, if so, add it\n vector<vector<int>> ret;\n if(root==NULL) return ret;\n vector<int> tmp;\n int movingSum=root->val;\n tmp.push_back(root->val);\n recur(root, targetSum, movingSum, ret, tmp);\n tmp.pop_back();\n return ret;\n }\n};", "memory": "16750" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n \npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n dfs(root, targetSum, 0);\n return result;\n }\n\n vector<vector<int>> result;\n vector<int> path;\n int currPathSum = 0;\n void dfs(TreeNode* node, int targetSum, int currPathSum) {\n if (!node) return;\n\n currPathSum += node->val;\n path.push_back(node->val);\n if (!node->left && !node->right) {\n if (currPathSum == targetSum) {\n vector<int> findPath(path.begin(), path.end());\n result.push_back(findPath);\n \n }\n path.pop_back();\n return;\n }\n\n dfs(node->left, targetSum, currPathSum);\n // path.pop_back();\n \n \n // path.push_back(node->val);\n dfs(node->right, targetSum, currPathSum);\n path.pop_back();\n }\n};", "memory": "17050" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void hasPathSum(TreeNode* root, int targetSum,vector<vector<int>>&res,vector<int>&smallAns) {\n if(root==NULL){\n return ;\n }\n if(root->left==NULL && root->right==NULL){\n if(targetSum-root->val ==0){\n vector<int>base(smallAns);\n base.push_back(root->val);\n res.push_back(base);\n }\n return;\n }\n smallAns.push_back(root->val);\n hasPathSum(root->left,targetSum-root->val,res,smallAns);\n hasPathSum(root->right,targetSum-root->val,res,smallAns);\n smallAns.pop_back();\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>>res;\n vector<int>smallAns;\n hasPathSum(root,targetSum,res,smallAns);\n return res;\n }\n};", "memory": "17350" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n deque<int> dq;\n vector<vector<int>> ans;\npublic:\n void inorderTraverse(TreeNode* root, int targetSum)\n {\n if(!root->left && !root->right && root->val == targetSum)\n {\n dq.push_back(targetSum);\n int dqSize = dq.size();\n vector <int> path;\n for(int i = 0; i < dqSize; ++i)\n {\n int front = dq.front();\n path.push_back(front);\n dq.pop_front();\n dq.push_back(front);\n } \n ans.push_back(path);\n dq.pop_back();\n return;\n } \n dq.push_back(root->val);\n if(root->left) \n inorderTraverse(root->left, targetSum - root->val);\n if(root->right)\n inorderTraverse(root->right, targetSum - root->val);\n dq.pop_back();\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n if(!root)\n return vector<vector<int>>();\n inorderTraverse(root, targetSum);\n return ans; \n }\n};", "memory": "17650" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>> ans;\n vector<int> path;\n\n if(root == nullptr) return ans;\n\n helper(ans, path, 0, targetSum, root);\n\n return ans;\n }\n\n void helper(vector<vector<int>>& ans, vector<int>& path, int currSum, int targetSum, TreeNode* root){\n int value = root->val;\n path.push_back(value);\n currSum += value;\n\n if(root->left == nullptr && root->right == nullptr && currSum == targetSum){\n ans.push_back(path);\n }else{\n if(root->left != nullptr){\n helper(ans, path, currSum, targetSum, root->left);\n }\n\n if(root->right != nullptr){\n helper(ans, path, currSum, targetSum, root->right);\n }\n }\n\n path.pop_back();\n }\n};", "memory": "17950" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<int> tempPath;\n vector<vector<int>> result;\n pathSumDFS(root, targetSum, tempPath, result);\n return result;\n }\n void pathSumDFS(TreeNode* root, int targetSum, vector<int>& tempPath, vector<vector<int>>& result) {\n if (root == nullptr) {\n return;\n }\n tempPath.push_back(root->val);\n if (root->left==nullptr && root->right==nullptr) {\n //leaf\n if (sum(tempPath) == targetSum) {\n\n result.push_back(tempPath);\n }\n tempPath.pop_back();\n return;\n }\n pathSumDFS(root->left, targetSum, tempPath, result);\n pathSumDFS(root->right, targetSum, tempPath, result);\n //remember!!\n tempPath.pop_back();\n }\n int sum(vector<int> tempPath) {\n int result = 0;\n for (auto& n : tempPath) {\n result += n;\n }\n return result;\n }\n};", "memory": "18250" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n\n vector<vector<int>> res; //to store final result\n\n void traverse(TreeNode* root, vector <int> &path, int rem){\n if(!root) return; //return at NULL\n\n if(!root->left && !root->right){ //leaf node reached\n path.push_back(root->val); //do\n if(rem == root->val) res.push_back(path);\n //if remaining sum is same as value of root we have valid path\n path.pop_back(); // undo\n return;\n }\n\n int val = root->val; //subtract the contribution of this value from rem (remaining sum)\n path.push_back(val); //do\n traverse(root->left, path, rem-val); //recurse left subtree\n traverse(root->right, path, rem-val); //recurse right subtree\n path.pop_back(); //undo\n\n }\n\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n res.clear(); //reset res \n if(!root) return res; //if root itself NULL there are no paths\n vector <int> path; //to store the path\n traverse(root, path, targetSum);\n return res;\n }\n};\n\n\n\n\n\n\n\n\n\n \n\n\n \n \n \n", "memory": "18550" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<int> tempPath;\n vector<vector<int>> result;\n pathSumDFS(root, targetSum, tempPath, result);\n return result;\n }\n void pathSumDFS(TreeNode* root, int targetSum, vector<int>& tempPath, vector<vector<int>>& result) {\n if (root == nullptr) {\n return;\n }\n tempPath.push_back(root->val);\n if (root->left==nullptr && root->right==nullptr) {\n //leaf\n if (sum(tempPath) == targetSum) {\n\n result.push_back(tempPath);\n }\n tempPath.pop_back();\n return;\n }\n pathSumDFS(root->left, targetSum, tempPath, result);\n pathSumDFS(root->right, targetSum, tempPath, result);\n //remember!!\n tempPath.pop_back();\n }\n int sum(vector<int> tempPath) {\n int result = 0;\n for (auto& n : tempPath) {\n result += n;\n }\n return result;\n }\n};", "memory": "18850" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<int> tempPath;\n vector<vector<int>> result;\n pathSumDFS(root, targetSum, tempPath, result);\n return result;\n }\n void pathSumDFS(TreeNode* root, int targetSum, vector<int>& tempPath, vector<vector<int>>& result) {\n if (root == nullptr) {\n return;\n }\n tempPath.push_back(root->val);\n if (root->left==nullptr && root->right==nullptr) {\n //leaf\n if (sum(tempPath) == targetSum) {\n\n result.push_back(tempPath);\n }\n tempPath.pop_back();\n return;\n }\n pathSumDFS(root->left, targetSum, tempPath, result);\n pathSumDFS(root->right, targetSum, tempPath, result);\n tempPath.pop_back();\n }\n int sum(vector<int> tempPath) {\n int result = 0;\n for (auto& n : tempPath) {\n result += n;\n }\n return result;\n }\n};", "memory": "19150" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n std::vector<std::vector<int>> ans;\n if (root == nullptr) {\n return ans;\n }\n\n // node, path sum, parent index\n std::vector<std::pair<TreeNode*, std::pair<int, int>>> tree_path{{root, {root->val, 0}}};\n\n for (int i = 0; i < tree_path.size(); ++i) {\n auto [node, sum_parent] = tree_path[i];\n auto [sum, parent] = sum_parent;\n\n int size = tree_path.size();\n\n if (node->left != nullptr) {\n tree_path.push_back({node->left, {sum + node->left->val, i}});\n }\n if (node->right != nullptr) {\n tree_path.push_back({node->right, {sum + node->right->val, i}});\n }\n if (size == tree_path.size() && targetSum == sum) {\n int j = i;\n std::deque<int> node_values;\n while (j != 0) {\n int sum = tree_path[j].second.first, parent_idx = tree_path[j].second.second;\n int parent_sum = tree_path[parent_idx].second.first;\n node_values.push_front(sum - parent_sum);\n j = parent_idx;\n }\n node_values.push_front(tree_path[j].second.first);\n ans.push_back({node_values.begin(), node_values.end()});\n }\n }\n\n return ans;\n }\n};", "memory": "19450" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void recur(TreeNode * root,int tar,vector<int>&v,vector<vector<int>>&res){\n if(root==nullptr)return;\n if(root->left==nullptr && root->right==nullptr && tar-root->val==0){\n v.push_back(root->val);\n res.push_back(v);\n v.pop_back();\n return;\n }\n int x=root->val;\n v.push_back(x);\n recur(root->left,tar-x,v,res);\n v.pop_back();\n v.push_back(x);\n recur(root->right,tar-x,v,res);\n v.pop_back();\n }\n vector<vector<int>> pathSum(TreeNode* root, int tar) {\n vector<vector<int>>res;\n vector<int>v;\n recur(root,tar,v,res);\n return res;\n }\n};", "memory": "19750" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "static int speedup = []() {\n ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);\n return 0;\n}();\n\nclass Solution\n{\n vector<vector<int>> res;\n void dfs(TreeNode *root, int target, vector<int> &gen)\n {\n if (!root)\n return;\n int val = root->val;\n target -= val;\n gen.push_back(val);\n if (target == 0 and !root->left and !root->right) {\n res.push_back(gen);\n \n }\n\n dfs(root->left, target, gen);\n dfs(root->right, target, gen);\n\n gen.pop_back();\n }\n\npublic:\n vector<vector<int>> pathSum(TreeNode *root, int targetSum)\n {\n vector<int> gen;\n dfs(root, targetSum, gen);\n return res;\n }\n};", "memory": "20050" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>> res;\n if (root == nullptr) {\n return res;\n }\n vector<int> tmp;\n function<void(TreeNode*, int)> dfs = [&](TreeNode* node, int sum) {\n if (node->left == nullptr && node->right == nullptr) {\n if (sum + node->val == targetSum) {\n vector<int> copy = tmp;\n copy.push_back(node->val);\n res.push_back(copy);\n }\n return;\n }\n tmp.push_back(node->val);\n if (node->left != nullptr) {\n dfs(node->left, sum + node->val);\n }\n if (node->right != nullptr) {\n dfs(node->right, sum + node->val);\n }\n tmp.pop_back();\n };\n dfs(root, 0);\n return res;\n }\n};", "memory": "20350" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void solve(TreeNode *root,int targetSum,int sum,vector<TreeNode *> &Paths,unordered_map<TreeNode *,TreeNode *> &previous){\n\n sum += root->val;\n\n if(root->left == NULL && root->right == NULL){\n\n if(sum == targetSum){\n Paths.push_back(root);\n return;\n }\n else{\n return;\n }\n }\n\n if(root->left == NULL && root->right != NULL){\n previous[root->right] = root;\n solve(root->right,targetSum,sum,Paths,previous);\n }\n else if(root->left != NULL && root->right == NULL){\n previous[root->left] = root;\n solve(root->left,targetSum,sum,Paths,previous);\n }\n else{\n previous[root->left] = root;\n previous[root->right] = root;\n solve(root->left,targetSum,sum,Paths,previous);\n solve(root->right,targetSum,sum,Paths,previous);\n }\n }\n\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n\n if(root == NULL){\n return {};\n }\n\n unordered_map<TreeNode *,TreeNode *> previous;\n vector<TreeNode *> Paths;\n\n int sum = 0;\n\n solve(root,targetSum,sum,Paths,previous);\n\n vector<vector<int>> result(Paths.size());\n\n int swap;\n\n for(int i=0;i<Paths.size();i++){\n\n TreeNode *temp = Paths[i];\n\n while(temp != root){\n result[i].push_back(temp->val);\n temp = previous[temp];\n }\n\n result[i].push_back(temp->val);\n \n for(int j=0;j<(result[i].size()/2);j++){\n swap = result[i][j];\n result[i][j] = result[i][result[i].size() - 1 - j];\n result[i][result[i].size() - 1 - j] = swap;\n }\n }\n\n return result; \n }\n};", "memory": "20650" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n static vector<int> path;\n static int curSum = 0;\n if (root == nullptr) {\n return {};\n }\n curSum += root->val;\n path.push_back(root->val);\n if (root->left == nullptr && root->right == nullptr) {\n vector<vector<int>> res;\n if (curSum == targetSum && path.size()) {\n res.push_back(path);\n }\n curSum -= root->val;\n path.pop_back();\n return res;\n }\n vector<vector<int> > res_left, res_right;\n if (root->left != nullptr) {\n res_left = pathSum(root->left, targetSum);\n }\n if (root->right != nullptr) { \n res_right = pathSum(root->right, targetSum);\n }\n if (res_left.size() < res_right.size()) {\n swap(res_left, res_right);\n }\n for (auto elem : res_right) {\n res_left.push_back(elem);\n }\n path.pop_back();\n curSum -= root->val;\n return res_left;\n }\n};", "memory": "20650" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nprivate: \n void dfs(TreeNode* root, vector<int>& v, vector<vector<int>>& ans){\n if(root==NULL)return;\n //preOrder\n v.push_back(root->val);\n if(root->left==NULL&&root->right==NULL){ans.push_back(v);}\n dfs(root->left,v,ans); \n dfs(root->right,v,ans); \n v.pop_back();\n \n }\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int target) {\n vector<int> v;\n vector<vector<int>> ans;\n dfs(root,v,ans);\n vector<vector<int>> res;\n for(auto it:ans){\n vector<int> str;\n int s=0;\n for(int j=0;j<it.size();j++){\n str.push_back(it[j]);\n s+=it[j];\n }\n if(s==target)\n res.push_back(str);\n } \n return res; \n }\n};", "memory": "20950" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n if (root == nullptr) {\n return {};\n }\n vector<vector<int>> paths;\n std::queue<std::pair<TreeNode*, int>> q;\n q.push({root, root->val});\n std::unordered_map<TreeNode*, TreeNode*> parent;\n parent[root] = nullptr;\n while (!q.empty()) {\n auto [node, value] = q.front();\n q.pop();\n if (value == targetSum && node->left == nullptr && node->right == nullptr) {\n paths.push_back({}); \n std::vector<int> &r = paths.back();\n TreeNode* p = node;\n while (p != nullptr) {\n r.push_back(p->val);\n p = parent[p];\n }\n std::reverse(r.begin(), r.end());\n }\n if (node->left != nullptr) {\n parent[node->left] = node;\n q.push({node->left, value + node->left->val});\n }\n if (node->right != nullptr) {\n parent[node->right] = node;\n q.push({node->right, value + node->right->val});\n }\n }\n return paths;\n }\n};", "memory": "20950" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> v1;\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n if(root==nullptr) return {};\n v1.push_back(root->val);\n vector<vector<int>> l1;\n if(root->left==nullptr && root->right==nullptr && root->val==targetSum){\n l1= {v1};\n v1.pop_back();\n return l1;\n }\n l1 = pathSum(root->left, targetSum - root->val);\n vector<vector<int>> r1 = pathSum(root->right, targetSum - root->val);\n l1.insert( l1.end(), r1.begin(), r1.end());\n v1.pop_back();\n return l1; \n }\n};", "memory": "21250" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> ans;\n vector<int> hasPathSum(TreeNode* root, int targetSum) {\n if (!root){\n return vector<int> (0);\n }\n vector<int> frw;\n if (!root-> left && !root -> right){\n // cout << root->val << endl;\n if (targetSum == root->val){\n ans.push_back(vector<int> {root->val});\n frw.push_back(ans.size()-1);\n return frw;\n }\n }\n targetSum = targetSum - (root->val);\n // bool ret = false\n vector <int> fl, fr;\n \n if (root->left){\n fl = hasPathSum(root->left, targetSum);\n if (fl.size()!=0){\n for(auto k :fl){\n ans[k].push_back(root->val);\n frw.push_back(k);\n }\n }\n }\n if (root->right){\n fr = hasPathSum(root->right, targetSum);\n if (fr.size()!=0){\n for(auto k :fr){\n ans[k].push_back(root->val);\n frw.push_back(k);\n }\n }\n }\n return frw;\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n hasPathSum(root, targetSum);\n for(auto &t:ans){\n reverse(t.begin(), t.end());\n }\n return ans;\n }\n};", "memory": "21250" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n vector<vector<int>> DFS_targetNum( TreeNode* root, int targetNum, int nowNum ) {\n vector<vector<int>> resultPath ; \n if ( root == nullptr ) // initial check\n return resultPath ; // if no value return a empty ;\n\n vector<vector<int>> tempPath ;\n nowNum = nowNum + root->val ; // always have val ;\n if ( root->left == nullptr && root->right == nullptr && nowNum == targetNum ) {\n resultPath.push_back( { root->val } ) ;\n return resultPath ;\n } // if is leaf and sum is target\n\n resultPath = DFS_targetNum( root->left, targetNum, nowNum ) ;\n tempPath = DFS_targetNum( root->right, targetNum, nowNum ) ;\n resultPath.insert( resultPath.end(), tempPath.begin(), tempPath.end() ) ; // getTotalPath\n\n if ( resultPath.empty() == false ) {\n for( int i = 0 ; i < resultPath.size() ; i ++ ) {\n resultPath[i].insert( resultPath[i].begin(), root->val ) ;\n } // for total leftOnePath\n } // one or more path is return\n\n\n return resultPath ; //\n } // \n\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n return DFS_targetNum( root, targetSum, 0 ) ;\n }\n};", "memory": "22150" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n vector<vector<int>> DFS_targetNum( TreeNode* root, int targetNum, int nowNum ) {\n vector<vector<int>> resultPath ; \n if ( root == nullptr ) // initial check\n return resultPath ; // if no value return a empty ;\n\n // vector<vector<int>> leftPath ;\n // vector<vector<int>> rightPath ; jush only one vairable \n\n vector<vector<int>> tempPath ;\n nowNum = nowNum + root->val ; // always have val ;\n if ( root->left == nullptr && root->right == nullptr && nowNum == targetNum ) {\n vector<int> onePath ;\n onePath.push_back( root->val ) ;\n resultPath.push_back( onePath ) ;\n return resultPath ;\n } // if is leaf and sum is target\n\n resultPath = DFS_targetNum( root->left, targetNum, nowNum ) ;\n tempPath = DFS_targetNum( root->right, targetNum, nowNum ) ;\n resultPath.insert( resultPath.end(), tempPath.begin(), tempPath.end() ) ; // getTotalPath\n\n if ( resultPath.empty() == false ) {\n for( int i = 0 ; i < resultPath.size() ; i ++ ) {\n resultPath[i].insert( resultPath[i].begin(), root->val ) ;\n // resultPath.push_back( leftPath[i] ) ;\n } // for total leftOnePath\n } // one or more path is return\n\n /*\n rightPath = DFS_targetNum( root->right, targetNum, nowNum ) ;\n if ( rightPath.empty() == false ) {\n for( int i = 0 ; i < rightPath.size() ; i ++ ) {\n rightPath[i].insert( rightPath[i].begin(), root->val ) ;\n resultPath.push_back( rightPath[i] ) ;\n } // for total rightOnePath\n } // one or more path is return \n */\n\n return resultPath ; //\n } // \n\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n return DFS_targetNum( root, targetSum, 0 ) ;\n }\n};", "memory": "22150" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n if (!root) {\n return {};\n }\n\n if (!root->left && !root->right) {\n if (targetSum == root->val) {\n auto result = vector<int>{ };\n result.reserve(100);\n result.push_back(root->val);\n return vector<vector<int>>{ result };\n } else {\n return {};\n }\n } else {\n auto result = vector<vector<int>>{};\n\n if (root->left) {\n auto leftResult = pathSum(root->left, targetSum - root->val);\n for (auto& r : leftResult) {\n r.insert(begin(r), root->val);\n result.push_back(move(r));\n }\n }\n\n if (root->right) {\n auto rightResult = pathSum(root->right, targetSum - root->val);\n for (auto& r : rightResult) {\n r.insert(begin(r), root->val);\n result.push_back(move(r));\n }\n }\n\n return result;\n }\n }\n};", "memory": "22450" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n if (!root) {\n return {};\n }\n\n if (!root->left && !root->right) {\n if (targetSum == root->val) {\n auto result = vector<int>{ };\n result.reserve(100);\n result.push_back(root->val);\n return vector<vector<int>>{ result };\n } else {\n return {};\n }\n } else {\n auto result = vector<vector<int>>{};\n\n if (root->left) {\n auto leftResult = pathSum(root->left, targetSum - root->val);\n for (auto& r : leftResult) {\n r.insert(begin(r), root->val);\n result.push_back(move(r));\n }\n }\n\n if (root->right) {\n auto rightResult = pathSum(root->right, targetSum - root->val);\n for (auto& r : rightResult) {\n r.insert(begin(r), root->val);\n result.push_back(move(r));\n }\n }\n\n return result;\n }\n }\n};", "memory": "22450" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n //cout<<root->val<<endl;\n vector<vector<int>> answer;\n if(root == nullptr) return answer ;\n else if(root->left == nullptr && root->right == nullptr){\n vector<int> leaf_val;\n if(root->val == targetSum){\n leaf_val.push_back(targetSum);\n answer.push_back(leaf_val);\n }\n \n return answer;\n }\n else if(root->left == nullptr){\n answer = pathSum(root->right, targetSum - root->val);\n //Remove all empty vectors\n answer.erase(\n remove_if(answer.begin(), answer.end(), [](const vector<int>& v) {\n return v.empty(); // Condition: if the vector is empty\n }),\n answer.end()\n );\n // for(int i=0;i<answer.size();i++){\n // if(answer[i].empty()){\n // answer.erase(answer.begin()+i);\n // }\n // } \n if(answer.empty()) return answer;\n for(int i=0;i<answer.size();i++){\n answer[i].insert(answer[i].begin(), root->val);\n } \n return answer; \n }\n else if(root->right == nullptr){\n answer = pathSum(root->left, targetSum - root->val);\n answer.erase(\n remove_if(answer.begin(), answer.end(), [](const vector<int>& v) {\n return v.empty(); // Condition: if the vector is empty\n }),\n answer.end()\n );\n if(answer.empty()) return answer;\n for(int i=0;i<answer.size();i++){\n answer[i].insert(answer[i].begin(), root->val);\n } \n return answer;\n }\n else {\n vector<vector<int>> answer_right, answer_left;\n answer_left = pathSum(root->left, targetSum - root->val);\n answer_right = pathSum(root->right, targetSum - root->val);\n answer = answer_left;\n answer.insert(answer.end(),answer_right.begin(), answer_right.end());\n //cout<<\"hello\"<<answer.size()<<endl;\n //int size_vec = answer.size();\n answer.erase(\n remove_if(answer.begin(), answer.end(), [](const vector<int>& v) {\n return v.empty(); // Condition: if the vector is empty\n }),\n answer.end()\n );\n //cout<<\"hello again\"<<answer.size()<<endl;\n if(answer.empty()) return answer;\n for(int i=0;i<answer.size();i++){\n answer[i].insert(answer[i].begin(), root->val);\n } \n return answer;\n }\n\n }\n};", "memory": "22750" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> paths;\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n if(root==NULL)\n return paths;\n TreeNode* curr;\n queue<TreeNode*> q;\n int currsum;\n queue<int> qsum;\n vector<int> path;\n queue<vector<int>> qpath;\n path.push_back(root->val);\n q.push(root);\n qsum.push(root->val);\n qpath.push(path);\n while(!q.empty())\n {\n curr=q.front();\n q.pop();\n currsum=qsum.front();\n qsum.pop();\n path=qpath.front();\n qpath.pop();\n if(curr->left!=NULL)\n {\n q.push(curr->left);\n qsum.push(currsum+curr->left->val);\n path.push_back(curr->left->val);\n qpath.push(path);\n path.pop_back();\n }\n if(curr->right!=NULL)\n {\n q.push(curr->right);\n qsum.push(currsum+curr->right->val);\n path.push_back(curr->right->val);\n qpath.push(path);\n path.pop_back();\n }\n if(curr->left==NULL && curr->right==NULL && currsum==targetSum)\n {\n paths.push_back(path);\n }\n }\n return paths;\n }\n};", "memory": "23050" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<int> curr;\n return findPaths(root, targetSum, curr);\n }\n\nprivate:\n vector<vector<int>> findPaths(TreeNode* root, int targetSum, vector<int>& vec) {\n if (!root) {\n return {};\n } else if (!root->left && !root->right) {\n vector<int> path = vec;\n path.push_back(root->val);\n if (targetSum == root->val) {\n return {path};\n } else {\n return {};\n }\n }\n\n vec.push_back(root->val);\n vector<vector<int>> left = findPaths(root->left, targetSum - vec.back(), vec);\n vector<vector<int>> right = findPaths(root->right, targetSum - vec.back(), vec);\n vec.pop_back();\n left.insert(left.end(), right.begin(), right.end());\n return left;\n }\n};", "memory": "23050" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int sum(vector<int>vec){\n int s=0;\n for(int i=0;i<vec.size();i++){\n s+=vec[i];\n }\n return s;\n }\n vector<int>vec;\n void pathsum2(TreeNode*root,vector<vector<int>>&ans,int targetsum){\n if(!root)return;\n vec.push_back(root->val);\n if(sum(vec)==targetsum&&(!root->left&&!root->right)){\n ans.push_back(vec);\n vec.pop_back();\n return;\n }\n else if(!root->left&&!root->right){\n vec.pop_back();\n return;\n }\n else{\n pathsum2(root->left,ans,targetsum);\n pathsum2(root->right,ans,targetsum);\n vec.pop_back();\n }\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>>ans;\n pathsum2(root,ans,targetSum);\n return ans;\n }\n};", "memory": "23350" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n if (root == nullptr) {\n return {};\n }\n std::vector<TreeNode*> children;\n if (root->left) children.push_back(root->left);\n if (root->right) children.push_back(root->right);\n if (children.size() == 0) {\n if (root->val == targetSum) {\n return {{targetSum}};\n }\n }\n\n std::vector<std::vector<int>> paths;\n for (const auto& child: children) {\n auto child_paths = pathSum(child, targetSum - root->val);\n for (const auto& p: child_paths) {\n paths.push_back({root->val});\n auto& b = paths.back();\n b.insert(b.end(), p.begin(), p.end());\n }\n }\n return paths;\n }\n};", "memory": "23650" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void print_array(vector<int>nums){\n for(int i=0;i<nums.size();i++){\n cout<<nums[i]<<\" \";\n }\n cout<<endl;\n }\n void pathSumhelp(TreeNode* root, int targetSum,vector<int>&subans,vector<vector<int>>&ans,TreeNode*& rootroot){\n if(root==NULL){\n subans.push_back(0);\n return;\n }\n if(root!=rootroot){\n subans.push_back(root->val);\n }\n \n cout<<root->val<<\" \"<<targetSum<<endl;\n print_array(subans);\n \n if(root->left==NULL && root->right==NULL &&targetSum!=root->val){\n return;\n }\n if(root->left==NULL && root->right==NULL &&targetSum==root->val){\n ans.push_back(subans);\n return;\n }\n pathSumhelp(root->left,targetSum-root->val,subans,ans,rootroot);\n subans.pop_back();\n \n \n pathSumhelp(root->right,targetSum-root->val,subans,ans,rootroot);\n subans.pop_back();\n \n\n\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>>ans;\n if(root==NULL){\n return ans;\n }\n vector<int>subans;\n subans.push_back(root->val);\n pathSumhelp(root,targetSum,subans,ans,root);\n return ans;\n }\n};", "memory": "23950" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void print_array(vector<int>nums){\n for(int i=0;i<nums.size();i++){\n cout<<nums[i]<<\" \";\n }\n cout<<endl;\n }\n void pathSumhelp(TreeNode* root, int targetSum,vector<int>&subans,vector<vector<int>>&ans,TreeNode*& rootroot){\n if(root==NULL){\n subans.push_back(0);\n return;\n }\n if(root!=rootroot){\n subans.push_back(root->val);\n }\n \n cout<<root->val<<\" \"<<targetSum<<endl;\n print_array(subans);\n \n if(root->left==NULL && root->right==NULL &&targetSum!=root->val){\n return;\n }\n if(root->left==NULL && root->right==NULL &&targetSum==root->val){\n ans.push_back(subans);\n return;\n }\n pathSumhelp(root->left,targetSum-root->val,subans,ans,rootroot);\n subans.pop_back();\n \n \n pathSumhelp(root->right,targetSum-root->val,subans,ans,rootroot);\n subans.pop_back();\n \n\n\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>>ans;\n if(root==NULL){\n return ans;\n }\n vector<int>subans;\n subans.push_back(root->val);\n pathSumhelp(root,targetSum,subans,ans,root);\n return ans;\n }\n};", "memory": "23950" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int sum(vector<int>vec){\n int s=0;\n for(int i=0;i<vec.size();i++){\n s+=vec[i];\n }\n return s;\n }\n vector<int>vec;\n void pathsum2(TreeNode*root,vector<vector<int>>&ans,int targetsum){\n if(!root)return;\n vec.push_back(root->val);\n if(sum(vec)==targetsum&&(!root->left&&!root->right)){\n ans.push_back(vec);\n vec.pop_back();\n return;\n }\n else if(!root->left&&!root->right){\n vec.pop_back();\n return;\n }\n else{\n pathsum2(root->left,ans,targetsum);\n pathsum2(root->right,ans,targetsum);\n vec.pop_back();\n }\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>>ans;\n pathsum2(root,ans,targetSum);\n return ans;\n }\n};", "memory": "24250" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int sum(vector<int>vec){\n int s=0;\n for(int i=0;i<vec.size();i++){\n s+=vec[i];\n }\n return s;\n }\n vector<int>vec;\n void pathsum2(TreeNode*root,vector<vector<int>>&ans,int targetsum){\n if(!root)return;\n vec.push_back(root->val);\n if(sum(vec)==targetsum&&(!root->left&&!root->right)){\n ans.push_back(vec);\n vec.pop_back();\n return;\n }\n else if(!root->left&&!root->right){\n vec.pop_back();\n return;\n }\n else{\n pathsum2(root->left,ans,targetsum);\n pathsum2(root->right,ans,targetsum);\n vec.pop_back();\n }\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>>ans;\n pathsum2(root,ans,targetSum);\n return ans;\n }\n};", "memory": "24250" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n if (!root) return {};\n if (!root->left && !root->right) {\n if (root->val == targetSum)\n return {{root->val}};\n else return {};\n }\n vector<vector<int>> leftSum = pathSum(root->left, targetSum - root->val);\n vector<vector<int>> rightSum = pathSum(root->right, targetSum - root->val);\n vector<vector<int>> merged(leftSum.size() + rightSum.size(), vector<int>());\n for (int i=0; i < merged.size(); i++) {\n merged.at(i) = {root->val};\n if (i < leftSum.size())\n merged.at(i).insert(merged.at(i).end(), leftSum.at(i).begin(), leftSum.at(i).end());\n else\n merged.at(i).insert(merged.at(i).end(), rightSum.at(i - leftSum.size()).begin(), rightSum.at(i - leftSum.size()).end());\n }\n return merged;\n }\n};", "memory": "24550" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n if (root == nullptr) {\n return {};\n }\n vector<vector<int>> output;\n\n if (root->left != nullptr) {\n const auto left_leaf_paths = pathSum(root->left, targetSum - root->val);\n\n for (const auto &path: left_leaf_paths) {\n output.push_back({root->val});\n output[output.size() - 1].insert(output[output.size() - 1].end(), path.begin(), path.end());\n }\n }\n\n if (root->right != nullptr) {\n const auto right_leaf_paths = pathSum(root->right, targetSum - root->val);\n\n for (const auto &path: right_leaf_paths) {\n output.push_back({root->val});\n output[output.size() - 1].insert(output[output.size() - 1].end(), path.begin(), path.end());\n }\n }\n\n if (root->left == nullptr && root->right == nullptr) {\n if (targetSum == root->val) {\n output = {{root->val}};\n }\n }\n\n return output;\n }\n};", "memory": "24850" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n if (root == nullptr) return {};\n if (root->left == nullptr && root->right == nullptr && root->val == targetSum) return {{root->val}};\n vector<vector<int>> answer;\n if (root->left != nullptr) {\n vector<vector<int>> left = pathSum(root->left, targetSum - root->val);\n for (const auto& v : left) {\n answer.emplace_back();\n answer.back().push_back(root->val);\n answer.back().insert(answer.back().end(), v.begin(), v.end());\n }\n }\n if (root->right != nullptr) {\n vector<vector<int>> right = pathSum(root->right, targetSum - root->val);\n for (const auto& v : right) {\n answer.emplace_back();\n answer.back().push_back(root->val);\n answer.back().insert(answer.back().end(), v.begin(), v.end());\n }\n }\n return answer;\n }\n};", "memory": "25150" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool isleaf(TreeNode* node){\n return !node->left && !node->right;\n } \n vector<vector<int>> result;\n void dfs(TreeNode* root, vector<int>& path, int remain){\n if(!root)\n return;\n path.push_back(root->val); \n //remain -= root->val;\n if(isleaf(root) && root->val == remain){\n result.push_back(path);\n return;\n }\n vector<int> tmp = path;\n dfs(root->left, path, remain - root->val);\n dfs(root->right, tmp, remain - root->val);\n //path.pop_back();\n //remain += root->val; \n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n if(!root)\n return {};\n vector<int> path;\n dfs(root, path, targetSum);\n return result;\n }\n};", "memory": "25450" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n vector<vector<int>> result;\nprivate:\n void traverse(TreeNode* root, vector<int>& path, int sum, int targetSum){\n if (!root)\n return;\n sum += root->val;\n path.push_back(root->val);\n if (!root->left && !root->right && sum == targetSum)\n result.push_back(path);\n vector<int> path2 = path;\n if (root->left)\n traverse(root->left, path, sum, targetSum);\n if (root->right)\n traverse(root->right, path2, sum, targetSum);\n }\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n if (!root)\n return {};\n vector<int> path;\n traverse(root, path, 0, targetSum);\n return result;\n }\n};", "memory": "25750" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n vector<vector<int>> pathSum1(TreeNode* root, int targetSum) {\n if (!root) {\n return {};\n }\n\n auto target = targetSum - root->val;\n if (target == 0 && (!root->left && !root->right)) {\n return {{ root->val }};\n }\n\n auto paths1 = pathSum(root->left, target);\n auto paths2 = pathSum(root->right, target);\n auto paths = std::move(paths1);\n for_each(paths2.begin(), paths2.end(), [&paths] (auto& path2) {\n paths.push_back(std::move(path2));\n });\n\n vector<vector<int>> newPaths;\n for_each(paths.begin(), paths.end(), [&newPaths, val = root->val] (const auto& path) {\n newPaths.emplace_back(1, val);\n newPaths.back().insert(newPaths.back().end(),path.begin(), path.end());\n });\n\n return newPaths;\n }\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n auto paths = pathSum1(root, targetSum);\n // for_each(paths.begin(), paths.end(),[] (auto& path) {\n // reverse(path.begin(), path.end());\n // });\n return paths;\n }\n};", "memory": "26050" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "#include <vector>\n\nusing namespace std;\n\n// Definition for a binary tree node.\n\n\nclass Solution {\npublic:\n vector<vector<int>> solve(TreeNode* root, int targetSum, vector<int>& path) {\n vector<vector<int>> result;\n \n if (root == nullptr) {\n return result;\n }\n \n // Add the current node's value to the path\n path.push_back(root->val);\n \n // If it's a leaf node, check if the path sum equals the targetSum\n if (root->left == nullptr && root->right == nullptr) {\n int sum = 0;\n for (int i = 0; i < path.size(); i++) {\n sum += path[i];\n }\n \n if (sum == targetSum) {\n result.push_back(path);\n }\n } else {\n // Continue searching in the left and right subtree\n vector<vector<int>> leftPaths = solve(root->left, targetSum, path);\n vector<vector<int>> rightPaths = solve(root->right, targetSum, path);\n \n result.insert(result.end(), leftPaths.begin(), leftPaths.end());\n result.insert(result.end(), rightPaths.begin(), rightPaths.end());\n }\n \n // Backtrack by removing the current node's value from the path\n path.pop_back();\n \n return result;\n }\n \n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<int> path;\n return solve(root, targetSum, path);\n }\n};\n", "memory": "26350" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n #include <vector>\n using namespace std;\nclass Solution {\npublic:\n std::vector<std::vector<int>> dfs(TreeNode* node ,size_t sum, std::vector<int>& path)\n { \n std::vector<std::vector<int>> result; \n if (node == nullptr)\n {\n return {};\n } \n path.push_back(node->val);\n\n if (!node->left and !node->right and sum == node->val)\n {\n result.push_back(path);\n }\n else \n {\n auto leftSub = dfs(node->left , sum - node->val , path);\n auto rightSub = dfs(node->right, sum - node->val , path);\n\n result.insert(result.end() , leftSub.begin() , leftSub.end());\n result.insert(result.end() , rightSub.begin() , rightSub.end());\n }\n\n path.pop_back();\n\n return result; \n \n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n std::vector<int> path; \n return dfs(root , targetSum , path);\n }\n};", "memory": "26650" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n unordered_map<TreeNode*, bool> visited;\n vector<vector<int>> dfs(TreeNode* root, vector<int>& trace, int sum) {\n if (root == nullptr)\n return {};\n\n vector<vector<int>> ans;\n if (sum == root->val && root->left == nullptr && root->right == nullptr) {\n trace.emplace_back(sum);\n ans.emplace_back(trace);\n trace.pop_back();\n return ans;\n }\n\n int new_sum = sum - root->val;\n trace.emplace_back(root->val);\n auto left = dfs(root->left, trace, new_sum);\n auto right = dfs(root->right, trace, new_sum);\n trace.pop_back();\n ans.insert(ans.end(), left.begin(), left.end());\n ans.insert(ans.end(), right.begin(), right.end());\n return ans;\n }\n\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<int> trace;\n auto ans = dfs(root, trace, targetSum);\n if (ans.empty() || ans[0].empty())\n return {};\n return ans;\n }\n};\n\n", "memory": "26950" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n#include <stack>\n\nvector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n struct Sol {\n Sol(TreeNode* root) \n : cursor{root}\n , total{root->val} {\n vals.push_back(root->val);\n }\n\n void update(TreeNode* next) {\n cursor = next;\n vals.push_back(next->val);\n total += next->val;\n }\n\n TreeNode* cursor;\n int total;\n vector<int> vals;\n };\n vector<vector<int>> res;\n if(root) {\n stack<Sol> open;\n open.emplace(root);\n while (!open.empty()) {\n auto top = std::move(open.top());\n if(top.cursor->left == nullptr && top.cursor->right == nullptr && top.total == targetSum) {\n res.push_back(top.vals);\n }\n open.pop();\n if(top.cursor->left) {\n auto& next = open.emplace(top);\n next.update(top.cursor->left);\n }\n if(top.cursor->right) {\n auto& next = open.emplace(top);\n next.update(top.cursor->right);\n }\n }\n }\n return res;\n}\n};", "memory": "27250" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nvector<vector<int>> ans;\nvector<int>k;\npair<int,vector<int>> p = make_pair(0,k);\npair<int,vector<int>> sum(TreeNode* t, int target) {\n if(t == NULL) {\n // if(p.first == target) {\n // ans.push_back(p.second);\n // }\n return p;\n }\n p.first = p.first + t->val;\n p.second.push_back(t->val);\n if(t!=NULL && t->left==NULL&&t->right==NULL) {\n if(p.first == target) {\n ans.push_back(p.second);\n }\n p.first = p.first - t->val;\n p.second.pop_back();\n return p;\n }\n \n sum(t->left,target);\n sum(t->right,target);\n // if(p.first == target) {\n // ans.push_back(p.second);\n // } \n p.first = p.first - t->val;\n p.second.pop_back();\n return p;\n}\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>>k;\n if(root == NULL) return k;\n sum(root,targetSum);\n return ans;\n }\n};", "memory": "27550" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nvector<vector<int>> ans;\nvector<int>k;\npair<int,vector<int>> p = make_pair(0,k);\npair<int,vector<int>> sum(TreeNode* t, int target) {\n if(t == NULL) {\n return p;\n }\n p.first = p.first + t->val;\n p.second.push_back(t->val);\n if(t!=NULL && t->left==NULL&&t->right==NULL) {\n if(p.first == target) {\n ans.push_back(p.second);\n }\n p.first = p.first - t->val;\n p.second.pop_back();\n return p;\n }\n \n sum(t->left,target);\n sum(t->right,target);\n p.first = p.first - t->val;\n p.second.pop_back();\n return p;\n}\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>>k;\n if(root == NULL) return k;\n sum(root,targetSum);\n return ans;\n }\n};", "memory": "27550" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>> vv = pathSumHelper(root, targetSum);\n for (auto &v: vv)\n {\n reverse(v.begin(), v.end());\n }\n\n return vv;\n }\n vector<vector<int>> pathSumHelper(TreeNode* root, int targetSum)\n {\n vector<vector<int>> vv;\n if (root == NULL)\n {\n return vv;\n }\n\n if (root->left == NULL && root->right == NULL && root->val == targetSum)\n {\n vv.push_back(vector<int>(1, root->val));\n return vv;\n }\n\n vector<vector<int>> vLeft = pathSumHelper(root->left, targetSum - root->val);\n vector<vector<int>> vRight = pathSumHelper(root->right, targetSum - root->val);\n\n if (vLeft.size() > 0)\n {\n vv.insert(vv.end(), vLeft.begin(), vLeft.end());\n }\n\n if (vRight.size() > 0)\n {\n vv.insert(vv.end(), vRight.begin(), vRight.end());\n }\n\n if (vv.size() > 0)\n {\n for (auto &v: vv)\n {\n v.push_back(root->val);\n }\n }\n\n return vv;\n }\n};", "memory": "27850" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> dfs(TreeNode* root, int targetSum) { \n vector<vector<int>> res;\n //null node\n if(!root) \n return res;\n\n //leaf node\n if(!root->left and !root->right) {\n if(targetSum == root->val) {\n res.push_back({root->val});\n }\n return res;\n }\n\n auto sub = dfs(root->left, targetSum-root->val);\n res.insert(res.end(), sub.begin(), sub.end());\n sub = dfs(root->right, targetSum-root->val);\n res.insert(res.end(), sub.begin(), sub.end());\n\n for(auto& v : res) {\n v.push_back(root->val);\n }\n\n return res;\n\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n auto res = dfs(root, targetSum);\n for(auto& v : res) {\n reverse(v.begin(), v.end());\n }\n return res;\n \n }\n};", "memory": "28150" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> dfs(TreeNode* root, int targetSum) { \n vector<vector<int>> res;\n //null node\n if(!root) \n return res;\n\n //leaf node\n if(!root->left and !root->right) {\n if(targetSum == root->val) {\n res.push_back({root->val});\n }\n return res;\n }\n\n auto sub = dfs(root->left, targetSum-root->val);\n res.insert(res.end(), sub.begin(), sub.end());\n sub = dfs(root->right, targetSum-root->val);\n res.insert(res.end(), sub.begin(), sub.end());\n\n for(auto& v : res) {\n v.push_back(root->val);\n }\n\n return res;\n\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n auto res = dfs(root, targetSum);\n for(auto& v : res) {\n reverse(v.begin(), v.end());\n }\n return res;\n \n }\n};", "memory": "28150" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>> vv = pathSumHelper(root, targetSum);\n for (auto &v: vv)\n {\n reverse(v.begin(), v.end());\n }\n\n return vv;\n }\n vector<vector<int>> pathSumHelper(TreeNode* root, int targetSum)\n {\n vector<vector<int>> vv;\n if (root == NULL)\n {\n return vv;\n }\n\n if (root->left == NULL && root->right == NULL && root->val == targetSum)\n {\n vv.push_back(vector<int>(1, root->val));\n return vv;\n }\n\n vector<vector<int>> vLeft = pathSumHelper(root->left, targetSum - root->val);\n vector<vector<int>> vRight = pathSumHelper(root->right, targetSum - root->val);\n\n if (vLeft.size() > 0)\n {\n vv.insert(vv.end(), vLeft.begin(), vLeft.end());\n }\n\n if (vRight.size() > 0)\n {\n vv.insert(vv.end(), vRight.begin(), vRight.end());\n }\n\n if (vv.size() > 0)\n {\n for (auto &v: vv)\n {\n v.push_back(root->val);\n }\n }\n\n return vv;\n }\n};", "memory": "28450" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>> vv = pathSumHelper(root, targetSum);\n for (auto &v: vv)\n {\n reverse(v.begin(), v.end());\n }\n\n return vv;\n }\n vector<vector<int>> pathSumHelper(TreeNode* root, int targetSum)\n {\n vector<vector<int>> vv;\n if (root == NULL)\n {\n return vv;\n }\n\n if (root->left == NULL && root->right == NULL && root->val == targetSum)\n {\n vv.push_back(vector<int>(1, root->val));\n return vv;\n }\n\n vector<vector<int>> vLeft = pathSumHelper(root->left, targetSum - root->val);\n vector<vector<int>> vRight = pathSumHelper(root->right, targetSum - root->val);\n\n if (vLeft.size() > 0)\n {\n vv.insert(vv.end(), vLeft.begin(), vLeft.end());\n }\n\n if (vRight.size() > 0)\n {\n vv.insert(vv.end(), vRight.begin(), vRight.end());\n }\n\n if (vv.size() > 0)\n {\n for (auto &v: vv)\n {\n v.push_back(root->val);\n }\n }\n\n return vv;\n }\n};", "memory": "28450" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> dfs(TreeNode* root, int targetSum) { \n vector<vector<int>> res;\n //null node\n if(!root) \n return res;\n\n //leaf node\n if(!root->left and !root->right) {\n if(targetSum == root->val) {\n res.push_back({root->val});\n }\n return res;\n }\n\n auto sub = dfs(root->left, targetSum-root->val);\n res.insert(res.end(), sub.begin(), sub.end());\n sub = dfs(root->right, targetSum-root->val);\n res.insert(res.end(), sub.begin(), sub.end());\n\n for(auto& v : res) {\n v.push_back(root->val);\n }\n\n return res;\n\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n auto res = dfs(root, targetSum);\n for(auto& v : res) {\n reverse(v.begin(), v.end());\n }\n return res;\n \n }\n};", "memory": "28750" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum1(TreeNode* root, int targetSum) {\n vector<vector<int>> output;\n if (!root) return output;\n if (!root->left && !root->right && root->val == targetSum) {\n output.push_back({root->val});\n return output; \n }\n if (root->left) {\n auto lv = pathSum1(root->left, targetSum - root->val);\n for (auto& v: lv) {\n v.push_back(root->val);\n output.push_back(v);\n }\n }\n if (root->right) {\n auto rv = pathSum1(root->right, targetSum - root->val);\n for (auto& v: rv) {\n v.push_back(root->val);\n output.push_back(v);\n }\n }\n return output;\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n auto output = pathSum1(root, targetSum);\n for (auto& v: output) {\n reverse(v.begin(), v.end());\n }\n return output;\n }\n};", "memory": "29050" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n void solve(TreeNode* root, vector<int>& curr, vector<vector<int>>& res, int sum, int targetSum) {\n if(!root->left && !root->right) {\n if(sum == targetSum)\n res.push_back(curr);\n }\n vector<int> v;\n if(root->left) {\n v = curr;\n v.push_back(root->left->val);\n solve(root->left, v, res, sum + root->left->val, targetSum);\n }\n if(root->right) {\n v = curr;\n v.push_back(root->right->val);\n solve(root->right, v, res, sum + root->right->val, targetSum);\n }\n }\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n if(!root)\n return {};\n vector<vector<int>> res;\n vector<int> curr = {root->val};\n solve(root, curr, res, root->val, targetSum);\n return res;\n }\n};", "memory": "29350" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>> res;\n if(!root) return res;\n if(!root->left && !root->right) {\n if(root->val == targetSum)\n res.push_back(vector<int>{root->val});\n return res;\n }\n auto left_res = pathSum(root->left, targetSum-root->val);\n auto right_res = pathSum(root->right, targetSum-root->val);\n res.reserve(left_res.size()+right_res.size());\n for(vector<int>& p : left_res) {\n p.insert(p.begin(), root->val);\n // res.push_back(p);\n }\n for(vector<int>& p : right_res) {\n p.insert(p.begin(), root->val);\n // res.push_back(p);\n }\n res.insert(res.end(), left_res.begin(), left_res.end());\n res.insert(res.end(), right_res.begin(), right_res.end());\n\n return res;\n }\n};", "memory": "29650" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n auto && rev = pathSumRev(root, targetSum);\n for (auto& it : rev) {\n std::reverse(it.begin(), it.end());\n }\n return rev;\n }\n vector<vector<int>> pathSumRev(TreeNode* root, int targetSum) {\n vector<vector<int>> ret;\n if(root == nullptr) {\n return ret;\n } else if (root->val == targetSum && root->right == nullptr && root->left == nullptr) {\n vector<int> inner;\n inner.push_back(root->val);\n ret.push_back(inner);\n return ret;\n } else {\n auto &&left = pathSumRev(root->left, targetSum - root->val);\n auto &&right = pathSumRev(root->right, targetSum - root->val);\n for (auto& it : left) {\n it.push_back(root->val);\n ret.push_back(it);\n }\n for (auto& it : right) {\n it.push_back(root->val);\n ret.push_back(it);\n }\n return ret;\n }\n }\n\n};", "memory": "29950" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution \n{\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum)\n {\n \n if (root == nullptr)\n {\n vector<vector<int>> temp;\n return temp;\n }\n\n else if(root->val == targetSum && root->left == nullptr && root->right == nullptr)\n {\n vector<vector<int>> simp;\n simp.push_back({root->val});\n return simp;\n }\n\n vector<vector<int>> l = pathSum(root->left, targetSum - root->val);\n vector<vector<int>> r = pathSum(root->right, targetSum - root->val);\n vector<vector<int>> f;\n if (l.size() != 0)\n {\n for (int i = 0; i < l.size(); i++)\n {\n l[i].insert(l[i].begin(), root->val);\n f.push_back(l[i]);\n }\n }\n if (r.size() != 0)\n {\n for (int i = 0; i < r.size(); i++)\n {\n r[i].insert(r[i].begin(), root->val);\n f.push_back(r[i]);\n }\n }\n \n return f;\n \n }\n};", "memory": "30250" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution \n{\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum)\n {\n \n if (root == nullptr)\n {\n vector<vector<int>> temp;\n return temp;\n }\n\n else if(root->val == targetSum && root->left == nullptr && root->right == nullptr)\n {\n vector<vector<int>> simp;\n simp.push_back({root->val});\n return simp;\n }\n\n vector<vector<int>> l = pathSum(root->left, targetSum - root->val);\n vector<vector<int>> r = pathSum(root->right, targetSum - root->val);\n vector<vector<int>> f;\n if (l.size() != 0)\n {\n for (int i = 0; i < l.size(); i++)\n {\n l[i].insert(l[i].begin(), root->val);\n f.push_back(l[i]);\n }\n }\n if (r.size() != 0)\n {\n for (int i = 0; i < r.size(); i++)\n {\n r[i].insert(r[i].begin(), root->val);\n f.push_back(r[i]);\n }\n }\n \n return f;\n \n }\n};", "memory": "30250" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution \n{\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum)\n {\n if (root == nullptr)\n {\n vector<vector<int>> temp;\n return temp;\n }\n\n else if(root->val == targetSum && root->left == nullptr && root->right == nullptr)\n {\n vector<vector<int>> simp;\n simp.push_back({root->val});\n return simp;\n }\n\n vector<vector<int>> l = pathSum(root->left, targetSum - root->val);\n vector<vector<int>> r = pathSum(root->right, targetSum - root->val);\n vector<vector<int>> f;\n if (l.size() != 0)\n {\n for (int i = 0; i < l.size(); i++)\n {\n l[i].insert(l[i].begin(), root->val);\n f.push_back(l[i]);\n }\n }\n if (r.size() != 0)\n {\n for (int i = 0; i < r.size(); i++)\n {\n r[i].insert(r[i].begin(), root->val);\n f.push_back(r[i]);\n }\n }\n \n return f;\n }\n};", "memory": "30550" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n //vector<vector<int>>\n\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>> ans;\n\n if(root == NULL)\n return ans;\n if(root->left == NULL & root->right == NULL) {\n if(root->val == targetSum) {\n vector<int> temp;\n temp.push_back(root->val);\n ans.push_back(temp);\n return ans;\n }\n }\n\n int newTarget = targetSum - root->val;\n vector<vector<int>> ansLeft = pathSum(root->left, newTarget);\n vector<vector<int>> ansRight = pathSum(root->right, newTarget);\n\n for(int i = 0; i < ansLeft.size(); i++) {\n ansLeft[i].insert(ansLeft[i].begin(), root->val);\n ans.push_back(ansLeft[i]);\n }\n for(int i = 0; i < ansRight.size(); i++) {\n ansRight[i].insert(ansRight[i].begin(), root->val);\n ans.push_back(ansRight[i]);\n }\n\n return ans;\n }\n};", "memory": "30850" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n vector<vector<int>> DFS_targetNum( TreeNode* root, int targetNum, int nowNum ) {\n vector<vector<int>> leftPath ;\n vector<vector<int>> rightPath ;\n vector<vector<int>> resultPath ;\n if ( root == nullptr ) \n return resultPath ; // if no value return a empty ;\n\n nowNum = nowNum + root->val ; // always have val ;\n if ( root->left == nullptr && root->right == nullptr && nowNum == targetNum ) {\n vector<int> onePath ;\n onePath.push_back( root->val ) ;\n resultPath.push_back( onePath ) ;\n return resultPath ;\n } // if is leaf and sum is target\n leftPath = DFS_targetNum( root->left, targetNum, nowNum ) ;\n if ( leftPath.empty() == false ) {\n for( int i = 0 ; i < leftPath.size() ; i ++ ) {\n leftPath[i].insert( leftPath[i].begin(), root->val ) ;\n resultPath.push_back( leftPath[i] ) ;\n } // for total leftOnePath\n } // one or more path is return\n\n\n rightPath = DFS_targetNum( root->right, targetNum, nowNum ) ;\n if ( rightPath.empty() == false ) {\n for( int i = 0 ; i < rightPath.size() ; i ++ ) {\n rightPath[i].insert( rightPath[i].begin(), root->val ) ;\n resultPath.push_back( rightPath[i] ) ;\n } // for total rightOnePath\n } // one or more path is return \n\n return resultPath ; //\n } // r\n \n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n return DFS_targetNum( root, targetSum, 0 ) ;\n }\n};", "memory": "31150" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n void append(TreeNode* root, int targetSum, vector<int> current, vector<vector<int>>& result)\n {\n //assert(root);\n\n current.push_back(root->val);\n\n if (!root->left && !root->right && targetSum == root->val) {\n result.push_back(std::move(current));\n return;\n }\n\n if (root->left) {\n auto&& curr = root->right ? current : std::move(current);\n append(root->left, targetSum - root->val, curr, result);\n }\n\n if (root->right) {\n append(root->right, targetSum - root->val, std::move(current), result);\n } \n }\n\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n if (!root) return {};\n\n vector<vector<int>> result;\n\n vector<int> current({root->val});\n\n if (!root->left && !root->right && targetSum == root->val) {\n result.push_back(std::move(current));\n return result;\n }\n\n if (root->left) {\n append(root->left, targetSum - root->val, current, result);\n }\n\n if (root->right) {\n append(root->right, targetSum - root->val, std::move(current), result);\n } \n\n return result;\n }\n};", "memory": "31150" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n std::vector<std::vector<int>> pathSum(TreeNode* root, int targetSum) {\n std::vector<std::vector<int>> result;\n std::stack<std::tuple<TreeNode*, int, std::vector<int>>> stack;\n if (root) {\n std::vector<int> path;\n path.push_back(root->val);\n stack.emplace(root, root->val, path);\n }\n while (!stack.empty()) {\n auto [current, sum, path] = stack.top();\n stack.pop();\n if (sum == targetSum && !current->left && !current->right) {\n result.emplace_back(path);\n continue;\n }\n if (current->left) {\n path.push_back(current->left->val);\n stack.emplace(current->left, sum + current->left->val, path);\n path.pop_back();\n }\n if (current->right) {\n path.push_back(current->right->val);\n stack.emplace(current->right, sum + current->right->val, path);\n }\n }\n return result;\n }\n};\n", "memory": "31450" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>> resVec;\n if(root == NULL)\n return resVec;\n \n vector<int> ivec;\n stack<std::tuple<TreeNode*, int, vector<int>>> st;\n ivec.push_back(root->val);\n st.push(std::make_tuple(root, root->val, ivec));\n\n while(!st.empty()){\n auto [node, currentSum, ivec] = st.top();\n st.pop();\n \n if(node->left == nullptr && node->right == nullptr && currentSum == targetSum) {\n // ivec.push_back(currentSum);\n resVec.push_back(ivec);\n ivec.clear();\n }\n\n if(node->left != nullptr) {\n ivec.push_back(node->left->val);\n st.push(std::make_tuple(node->left, currentSum + node->left->val, ivec));\n ivec.pop_back();\n }\n\n if(node->right != nullptr) {\n ivec.push_back(node->right->val);\n st.push(std::make_tuple(node->right, currentSum + node->right->val, ivec));\n ivec.pop_back();\n }\n }\n\n return resVec;\n }\n};", "memory": "31450" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n using path_t = std::vector<int>;\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n \n\n if (!root)\n return {};\n\n std::stack<std::tuple<TreeNode*, int, path_t>> stack;\n vector<vector<int>> result;\n \n stack.push({root, 0, {}});\n\n while(!stack.empty())\n {\n auto triple = stack.top();\n stack.pop();\n\n auto* node = std::get<TreeNode*>(triple);\n auto sum = std::get<int>(triple);\n auto& path = std::get<path_t>(triple);\n\n sum += node->val;\n path.push_back(node->val);\n\n if (node->right)\n {\n stack.push({node->right, sum, path});\n }\n\n if (node->left)\n {\n stack.push({node->left, sum, path});\n }\n\n if (!node->left && !node->right)\n {\n if (sum == targetSum)\n result.push_back(std::move(path));\n }\n }\n\n return result;\n }\n};", "memory": "31750" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n if (!root) return {};\n\n stack<tuple<TreeNode*, vector<int>, int>> runningPaths;\n runningPaths.push({root, {}, 0});\n vector<vector<int>> allPaths;\n while (!runningPaths.empty()) {\n auto [node, path, previousSum] = runningPaths.top();\n runningPaths.pop();\n path.push_back(node->val);\n int newSum = node->val + previousSum;\n if (!node->left && !node->right && newSum == targetSum) {\n allPaths.push_back(path);\n } else {\n if (node->left) runningPaths.push({node->left, path, newSum});\n if (node->right) runningPaths.push({node->right, path, newSum});\n }\n }\n return allPaths;\n }\n};", "memory": "31750" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void func(TreeNode* root, int target, vector<int> curr, vector<vector<int>> &ans){\n if(root == NULL) return;\n if(root->left == NULL && root->right == NULL){\n if(target == root->val){\n curr.push_back(root->val);\n ans.push_back(curr);\n }\n return;\n }\n curr.push_back(root->val);\n if(root->left != NULL){\n func(root->left,target-root->val,curr,ans);\n }\n if(root->right != NULL){\n func(root->right,target-root->val,curr,ans);\n }\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>> ans;\n func(root,targetSum,{},ans);\n return ans;\n }\n};", "memory": "32050" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nprivate:\n \n void dfs(TreeNode* curr, vector<vector<int>>& ret, vector<int>& path, int sum_left) {\n if (sum_left == 0 && (curr->left == nullptr && curr->right == nullptr)) {\n ret.push_back(path);\n return;\n }\n if (curr == nullptr) return;\n \n vector<int> path2;\n for (int i = 0; i < path.size(); i++) path2.push_back(path[i]);\n \n if (curr->left) {\n path.push_back(curr->left->val);\n dfs(curr->left, ret, path, sum_left - curr->left->val);\n }\n \n if (curr->right) {\n path2.push_back(curr->right->val);\n dfs(curr->right, ret, path2, sum_left - curr->right->val);\n }\n \n }\n \npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>> ret;\n if (!root) return ret;\n \n vector<int> path;\n path.push_back(root->val);\n dfs(root, ret, path, targetSum - root->val);\n \n return ret;\n \n }\n};", "memory": "32050" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void traversal(TreeNode *root, int t,vector<int> temp,vector<vector<int>>&ans){\n \n if(t==0&&root->left==NULL&&root->right==NULL){\n ans.push_back(temp);\n return;\n }\n \n \n if(root->left!=NULL){\n temp.push_back(root->left->val);\n traversal(root->left,t-root->left->val,temp,ans);\n temp.pop_back();\n }\n \n if(root->right!=NULL){\n temp.push_back(root->right->val);\n traversal(root->right,t-root->right->val,temp,ans);\n temp.pop_back();\n }\n return;\n }\n \n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>> ans;\n vector<int> temp;\n if(root==NULL)\n return ans;\n temp.push_back(root->val);\n traversal(root,targetSum-root->val,temp,ans);\n return ans;\n\n \n \n \n }\n};", "memory": "32350" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n void sum(TreeNode* root, int targetSum,vector<vector<int>> &v,int value,vector<int> a){\n if(root->left==NULL && root->right==NULL){\n if(value+root->val == targetSum){\n a.push_back(root->val);\n v.push_back(a);\n }\n return;\n }\n a.push_back(root->val);\n if(root->left!=NULL){\n sum(root->left,targetSum,v,value+root->val,a);\n }\n if(root->right!=NULL){\n sum(root->right,targetSum,v,value+root->val,a);\n }\n\n return;\n }\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>> v;\n if(root==NULL){\n return v;\n }\n \n vector<int> a;\n sum(root,targetSum,v,0,a);\n return v;\n }\n};", "memory": "32350" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> ans;\n void recur(TreeNode* root,int currSum,int targetSum,vector<int>v){\n if(root==NULL)\n return;\n if(root->left==NULL and root->right==NULL){\n if(currSum+root->val==targetSum){\n v.push_back(root->val);\n ans.push_back(v);\n }\n return;\n }\n v.push_back(root->val);\n if(root->left){\n recur(root->left,currSum+root->val,targetSum,v);\n }\n if(root->right){\n recur(root->right,currSum+root->val,targetSum,v);\n }\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n recur(root,0,targetSum,{});\n return ans;\n }\n};", "memory": "32650" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n vector<vector<int>> ans;\n\n void dfs(TreeNode *node, int accSum, int targetSum, vector<int> nodes) {\n if(!node->left && !node->right){\n if(accSum + node->val == targetSum) {\n nodes.push_back(node->val);\n ans.push_back(nodes);\n }\n }\n if(node->left){\n nodes.push_back(node->val);\n dfs(node->left, accSum + node->val, targetSum, nodes);\n nodes.pop_back();\n }\n if(node->right){\n nodes.push_back(node->val);\n dfs(node->right, accSum + node->val, targetSum, nodes);\n nodes.pop_back();\n }\n }\n\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n if(root) dfs(root, 0, targetSum, vector<int>());\n return ans;\n }\n};", "memory": "32650" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n if (root == nullptr) return {};\n\n vector<vector<int>> sumPaths;\n vector<int> path;\n pathSumDFS(root, targetSum, sumPaths, path);\n return sumPaths;\n }\n\n void pathSumDFS(TreeNode* root, int targetSum, vector<vector<int>>& sumPaths, vector<int> path) {\n if (root->right == nullptr && root->left == nullptr) {\n path.push_back(root->val);\n if (accumulate(path.begin(), path.end(), 0) == targetSum) {\n sumPaths.push_back(path);\n }\n } else {\n path.push_back(root->val);\n if (root->left != nullptr) {\n pathSumDFS(root->left, targetSum, sumPaths, path);\n }\n if (root->right != nullptr) {\n pathSumDFS(root->right, targetSum, sumPaths, path);\n }\n }\n }\n};", "memory": "32950" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n void print_path(TreeNode* root, int target, int sum, vector<int> vec,\n vector<vector<int>>& con) {\n if (root != nullptr)\n sum += root->val;\n else\n return;\n vec.push_back(root->val);\n if (root->left)\n print_path(root->left, target, sum, vec, con);\n if (root->right)\n print_path(root->right, target, sum, vec, con);\n if (!root->left && !root->right) {\n if (sum == target) {\n con.push_back(vec);\n } else\n return;\n }\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<int> vec;\n vector<vector<int>> res;\n print_path(root, targetSum, 0, vec, res);\n return res;\n }\n};", "memory": "32950" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nprivate:\n void dfs(TreeNode *tn, int target, vector<int> path, vector<vector<int>> &paths) {\n target -= tn->val;\n path.push_back(tn->val);\n if (target == 0 && !tn->right && !tn->left) {\n paths.push_back(path);\n return;\n }\n if (tn->left) { dfs(tn->left, target, path, paths); }\n if (tn->right) { dfs(tn->right, target, path, paths); }\n path.pop_back();\n target += tn->val;\n }\n\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>> paths;\n if (!root) { return paths; }\n vector<int> path;\n dfs(root, targetSum, path, paths);\n return paths;\n }\n};", "memory": "33250" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void dfs(TreeNode* root, int targetSum, vector<int> path_buffer, vector<vector<int>>& result_buffer)\n {\n if(!root -> left && !root -> right)\n {\n int sum = 0;\n path_buffer.push_back(root->val);\n \n if(root->val == targetSum)\n {\n result_buffer.push_back(path_buffer);\n }\n }\n path_buffer.push_back(root->val);\n if(root->left)\n dfs(root->left, targetSum - root->val, path_buffer, result_buffer);\n if(root->right)\n dfs(root->right, targetSum - root->val, path_buffer, result_buffer);\n\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>> result;\n vector<int> path_buffer;\n if(!root)\n return result;\n dfs(root, targetSum, path_buffer, result);\n return result;\n }\n};", "memory": "33250" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> ans;\n void recur(TreeNode* root,int currSum,int targetSum,vector<int>v){\n if(root==NULL)\n return;\n if(root->left==NULL and root->right==NULL){\n if(currSum+root->val==targetSum){\n v.push_back(root->val);\n ans.push_back(v);\n }\n \n }\n v.push_back(root->val);\n if(root->left){\n recur(root->left,currSum+root->val,targetSum,v);\n }\n if(root->right){\n recur(root->right,currSum+root->val,targetSum,v);\n }\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n recur(root,0,targetSum,{});\n return ans;\n }\n};", "memory": "33550" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n #define node TreeNode\n #define pb push_back\n vector<vector<int>> pathSum(TreeNode* root, int x) {\n vector<vector<int>> ans;\n function<void(node*,int,vector<int>)> dfs = [&](node* root,int sum,vector<int> p){\n if(!root)return;\n if(!root->left && !root->right){\n sum += root->val;\n //cout<<\"sum: \"<<sum<<endl;\n //for(int i:p)cout<<i<<\" \";cout<<endl;\n if(sum==x){\n p.pb(root->val);\n ans.pb(p);\n }\n return;\n }\n p.pb(root->val);\n dfs(root->left,sum+root->val,p);\n dfs(root->right,sum+root->val,p);\n p.pop_back();\n };\n vector<int> p;\n dfs(root,0,p);\n return ans;\n }\n};", "memory": "33550" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nbool first = true;\n\nvector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n bool isFirst = first;\n first = false;\n if (!root) return vector<vector<int>>();\n if (!root->left && !root->right && root->val == targetSum) return vector<vector<int>>(1, vector<int>(1,root->val));\n vector<vector<int>> lAns = pathSum(root->left, targetSum - root->val), rAns = pathSum(root->right, targetSum - root->val);\n vector<vector<int>> ret;\n for (int i = 0, n = lAns.size(), m = rAns.size(); i < m+n; i++){\n vector<int> v = (i < n ? lAns[i] : rAns[i-n]);\n v.push_back(root->val);\n if (isFirst) reverse(v.begin(), v.end());\n ret.push_back(v);\n }\n return ret;\n}\n};", "memory": "33850" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void root_leaf (TreeNode*&node, vector<int> v, vector<vector<int>> &result){\n if (node->left == nullptr && node->right == nullptr)\n { \n v.push_back(node->val);\n result.push_back(v);\n }\n else{\n v.push_back(node->val);\n if (node->left) root_leaf(node->left, v, result);\n if (node->right) root_leaf(node->right, v, result);\n }\n\n\n}\nvector<vector<int>> pathSum(TreeNode *root, int targetSum)\n{\n vector<vector<int>> result;\n if (root == nullptr) return result;\n vector<vector<int>> temp;\n vector<int> dummy;\n root_leaf(root, dummy, temp);\n for (int i = 0; i < temp.size(); i++){\n int sum = 0;\n for (int j = 0; j < temp[i].size(); j++){\n sum += temp[i][j];\n }\n if (sum == targetSum){\n result.push_back(temp[i]);;\n }\n }\n return result;\n // vector<int> v;\n // if (hasPathSum(root, targetSum))\n // v.push_back(root->val);\n // if (hasPathSum(root->left, targetSum))\n // temp = pathSum(root->left, targetSum - root->val);\n // else\n // return result;\n}\n};", "memory": "33850" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<vector<int>> pathSum1(TreeNode* root, int targetSum) {\n vector<vector<int>> vect;\n if(root==NULL){return vect;}\n if((root->left==NULL)&&(root->right==NULL)){\n vector<int> temp;\n if(root->val==targetSum){temp.push_back(root->val);\n vect.push_back(temp);\n }\n return vect;\n\n }\n vector<vector<int>> left= pathSum1(root->left,targetSum-(root->val));\n vector<vector<int>> right= pathSum1(root->right,targetSum-(root->val));\n for(auto p:left){\n p.push_back(root->val);\n vect.push_back(p);\n }\n for(auto p:right){\n p.push_back(root->val);\n vect.push_back(p);\n }\n return vect;\n\n }\n vector<vector<int>> pathSum(TreeNode* root, int targetSum){\n vector<vector<int>> vect=pathSum1(root,targetSum);\n for(auto& p:vect){\n reverse(p.begin(),p.end());\n }\n return vect;\n }\n};", "memory": "34150" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n int target_ = 0;\n std::vector<std::vector<int>> results_;\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n target_ = targetSum;\n\n if(!root) {\n return {};\n }\n\n search(root, {}, 0);\n\n return results_;\n }\n\n void search(TreeNode* root, std::vector<int> path, int currentSum) {\n //std::cout << \"currentSum \" << currentSum << std::endl;\n if(!root) {\n //std::cout << \"ending at currentSum \" << currentSum << std::endl;\n if(currentSum == target_) {\n results_.push_back(path);\n }\n return;\n }\n\n path.push_back(root->val);\n\n if(!root->left) {\n search(root->right, path, currentSum + root->val);\n } else if(!root->right) {\n search(root->left, path, currentSum + root->val);\n } else {\n search(root->left, path, currentSum + root->val);\n search(root->right, path, currentSum + root->val);\n }\n }\n};", "memory": "34750" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>> res; // Stores all valid paths\n if (!root) return res; // Edge case: empty tree\n\n // Stack to store tuples (current_node, current_path, current_sum)\n stack<tuple<TreeNode*, vector<int>, int>> s;\n s.push({root, {root->val}, root->val});\n\n while (!s.empty()) {\n auto [node, path, current_sum] = s.top();\n s.pop();\n\n // Check if it's a leaf node and the path sum matches the target sum\n if (!node->left && !node->right && current_sum == targetSum) {\n res.push_back(path);\n }\n\n // Push the right child to the stack (if it exists)\n if (node->right) {\n vector<int> new_path = path;\n new_path.push_back(node->right->val);\n s.push({node->right, new_path, current_sum + node->right->val});\n }\n\n // Push the left child to the stack (if it exists)\n if (node->left) {\n vector<int> new_path = path;\n new_path.push_back(node->left->val);\n s.push({node->left, new_path, current_sum + node->left->val});\n }\n }\n\n return res;\n }\n};\n", "memory": "34750" }
113
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>all <strong>root-to-leaf</strong> paths where the sum of the node values in the path equals </em><code>targetSum</code><em>. Each path should be returned as a list of the node <strong>values</strong>, not node references</em>.</p> <p>A <strong>root-to-leaf</strong> path is a path starting from the root and ending at any leaf node. A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsumii1.jpg" style="width: 500px; height: 356px;" /> <pre> <strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 <strong>Output:</strong> [[5,4,11,2],[5,8,4,5]] <strong>Explanation:</strong> There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" style="width: 212px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,3], targetSum = 5 <strong>Output:</strong> [] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1,2], targetSum = 0 <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> <li><code>-1000 &lt;= targetSum &lt;= 1000</code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n // vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n // //if(root == nullptr) return root;\n // //create a vector to store result\n // vector<vector<int>>result;\n // //create a stack to store path\n // stack <TreeNode*> path;\n // //create a stack to store the sum of vales\n // stack <int> sum;\n\n // path.push(root);\n // sum.push(root->val);\n\n // while(!path.empty()){\n // int temp = sum.top();\n // TreeNode* tempNode = path.top();\n // path.pop();\n // sum.pop();\n\n // if(tempNode->left == nullptr && tempNode->right ==nullptr && tempNode->val == targetSum){\n // result.push_back(tempNode);\n // }\n\n // if(tempNode->right!=nullptr){\n // path.push(temp->right);\n // sum.push(temp + tempNode->right->val);\n // }\n // if(tempNode->left!=nullptr){\n // path.push(temp->left);\n // sum.push(temp + tempNode->left->val);\n // }\n // }\n\n // return result;\n\n vector<vector<int>> pathSum(TreeNode* root, int targetSum) {\n vector<vector<int>> result;\n\n if (root == nullptr) return result; // Base case: return empty result if root is nullptr\n \n // Stack for nodes, paths, and cumulative sums\n stack<pair<TreeNode*, vector<int>>> pathStack;\n stack<int> sumStack;\n\n // Initial push\n pathStack.push({root, {root->val}});\n sumStack.push(root->val);\n\n while (!pathStack.empty()) {\n // Get current node and path\n auto [currNode, currPath] = pathStack.top();\n pathStack.pop();\n int currSum = sumStack.top();\n sumStack.pop();\n\n // If it's a leaf node and the sum matches targetSum\n if (currNode->left == nullptr && currNode->right == nullptr && currSum == targetSum) {\n result.push_back(currPath);\n }\n\n // Check the right child\n if (currNode->right != nullptr) {\n vector<int> newPath = currPath;\n newPath.push_back(currNode->right->val);\n pathStack.push({currNode->right, newPath});\n sumStack.push(currSum + currNode->right->val);\n }\n\n // Check the left child\n if (currNode->left != nullptr) {\n vector<int> newPath = currPath;\n newPath.push_back(currNode->left->val);\n pathStack.push({currNode->left, newPath});\n sumStack.push(currSum + currNode->left->val);\n }\n }\n\n return result;\n }\n\n \n};", "memory": "35050" }