C++ Iterative solution


#1

vector ans;

stack<TreeNode*> s;

s.push(A);
while(!s.empty())
{
    TreeNode* root = s.top();
    ans.push_back(root->val);
    
    s.pop();
    
    if(root->right)
    s.push(root->right);
    if(root->left)
    s.push(root->left);
    
}

return ans;

}