Cpp easy solution without recursion

interview-questions
Tags: #<Tag:0x00007f242664f0b8>

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