<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>

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    // vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
    //     //if(root == nullptr) return root;
    //     //create a vector to store result
    //     vector<vector<int>>result;
    //     //create a stack to store path
    //     stack <TreeNode*> path;
    //     //create a stack to store the sum of vales
    //     stack <int> sum;

    //     path.push(root);
    //     sum.push(root->val);

    //     while(!path.empty()){
    //         int temp = sum.top();
    //         TreeNode* tempNode = path.top();
    //         path.pop();
    //         sum.pop();

    //         if(tempNode->left == nullptr && tempNode->right ==nullptr && tempNode->val == targetSum){
    //             result.push_back(tempNode);
    //         }

    //          if(tempNode->right!=nullptr){
    //             path.push(temp->right);
    //             sum.push(temp + tempNode->right->val);
    //         }
    //         if(tempNode->left!=nullptr){
    //             path.push(temp->left);
    //             sum.push(temp + tempNode->left->val);
    //         }
    //     }

    //     return result;

    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        vector<vector<int>> result;

        if (root == nullptr) return result; // Base case: return empty result if root is nullptr
        
        // Stack for nodes, paths, and cumulative sums
        stack<pair<TreeNode*, vector<int>>> pathStack;
        stack<int> sumStack;

        // Initial push
        pathStack.push({root, {root->val}});
        sumStack.push(root->val);

        while (!pathStack.empty()) {
            // Get current node and path
            auto [currNode, currPath] = pathStack.top();
            pathStack.pop();
            int currSum = sumStack.top();
            sumStack.pop();

            // If it's a leaf node and the sum matches targetSum
            if (currNode->left == nullptr && currNode->right == nullptr && currSum == targetSum) {
                result.push_back(currPath);
            }

            // Check the right child
            if (currNode->right != nullptr) {
                vector<int> newPath = currPath;
                newPath.push_back(currNode->right->val);
                pathStack.push({currNode->right, newPath});
                sumStack.push(currSum + currNode->right->val);
            }

            // Check the left child
            if (currNode->left != nullptr) {
                vector<int> newPath = currPath;
                newPath.push_back(currNode->left->val);
                pathStack.push({currNode->left, newPath});
                sumStack.push(currSum + currNode->left->val);
            }
        }

        return result;
    }

    
};
35050