Noice Solution using Unordered Map :-)


#1
vector<int> Solution::preorderTraversal(TreeNode* A) {


vector<int>ans;

unordered_map<TreeNode*,int > mp;

stack<TreeNode*>s;

s.push(A);

while(!s.empty())
{
    TreeNode * curr =s.top();
    
    if(curr==NULL)
    {
        s.pop();
        continue;
    }
    
    if(mp[curr]==0)
    ans.push_back(curr->val);
    else if(mp[curr]==1)
    s.push(curr->left);
    else if(mp[curr]==2)
    s.push(curr->right);
    else
    s.pop();
    
    mp[curr]++;
}

return ans;

}