Those who are having difficulty, see this two step solution


#1
vector<int> Solution::preorderTraversal(TreeNode* A)
{
    stack<TreeNode*>s;
    vector<int> v;
    
    //step 1
    TreeNode *curr=A;
    while(curr)
    {
        v.push_back(curr->val);
        s.push(curr);
        curr=curr->left;
    }
    
    //step 2
    while(!s.empty())
    {
        TreeNode *temp=s.top();
        s.pop();
        if(temp->right)
        {
            curr=temp->right;
            while(curr)
            {
                v.push_back(curr->val);
                s.push(curr);
                curr=curr->left;
            }
        }
    }
    return v;
}