Shortest and easy iterative c++ sol


#1

vector Solution::preorderTraversal(TreeNode* A) {
vector res;
stack<TreeNode*> s;
while(1)
{
if(A)
{
res.push_back(A->val);
s.push(A);
A=A->left;
}
else
{
if(s.empty())
return res;
A=s.top()->right;
s.pop();
}
}
return res;
}