<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</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/03/12/paths-tree.jpg" style="width: 207px; height: 293px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,null,5]
<strong>Output:</strong> [&quot;1-&gt;2-&gt;5&quot;,&quot;1-&gt;3&quot;]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> root = [1]
<strong>Output:</strong> [&quot;1&quot;]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[1, 100]</code>.</li>
	<li><code>-100 &lt;= Node.val &lt;= 100</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:

    //Intuition: we recursively do a preorder traversal for tree, whenever we reach any non leaf node, we add that to our current path and continue traversal
    //When we reach a leaf node, we create string from all the nodes in our current path (include leaf node also) and add it in our allPaths vector
    //When we backtrack (after completing all the downstream recursion calls) we remove the currNode from the current path.
    void traversePreorderWhileTrackingNodes(TreeNode* currNode, vector<int> currentPathNodes, vector<string>& allPaths){
        currentPathNodes.push_back(currNode->val);

        if(currNode->left == NULL && currNode->right == NULL){ //we have reached a leaf node
            string currentPath;
            //add the nodes (from root to currNode in a string)
            for(int i = 0; i < currentPathNodes.size(); ++i){
                currentPath+=to_string(currentPathNodes[i]); 
                if(i != currentPathNodes.size() - 1){ //if the current node is not the final node
                    currentPath+="->"; //add the arrow symbol
                }
            }
            allPaths.push_back(currentPath); //add the current path to our final paths
        }
        if(currNode->left != NULL){
           traversePreorderWhileTrackingNodes(currNode->left, currentPathNodes, allPaths);
        }
        if(currNode->right != NULL){
           traversePreorderWhileTrackingNodes(currNode->right, currentPathNodes, allPaths);
        }
        
        //while backtracking, remove the currNode from currentPathNodes so that it won't be added in next paths
        currentPathNodes.pop_back();       
    }

    //Intuition: same as above, we have just optimized it further to further squeeze down the runtime by passing string insted of vectot
    //and by passing by reference we are avoiding a copy creation at every step
    void traversePreorderWhileTrackingNodesOptimizedFurther(TreeNode* currNode, string& currentPath, vector<string>& allPaths){
        int originalCurrentPathLength = currentPath.length(); //we will use this to remove the newly added node and "->" during backtracking
        currentPath += to_string(currNode->val);
        if(currNode->left == NULL && currNode->right == NULL){ //we have reached a leaf node
            allPaths.push_back(currentPath); //add the current path to our final paths
        }
        //if we have reached a non leaf node we also need to add "->"
        currentPath += "->";
        if(currNode->left != NULL){
           traversePreorderWhileTrackingNodesOptimizedFurther(currNode->left, currentPath, allPaths);
        }
        if(currNode->right != NULL){
           traversePreorderWhileTrackingNodesOptimizedFurther(currNode->right, currentPath, allPaths);
        }
    
        //while backtracking, remove the currNode and "->" from currentPath so that it won't be added in next paths
        while(currentPath.length() > originalCurrentPathLength){ //while there are extra characters in string which we added in current function call
            currentPath.pop_back();
        }
    }

    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> allPaths;
        
        //vector<int> currentPathNodes;
        //traversePreorderWhileTrackingNodes(root, currentPathNodes, allPaths);
        
        string currentPath;
        traversePreorderWhileTrackingNodesOptimizedFurther(root, currentPath, allPaths);
        
        return allPaths;


    }
};
15200