Cpp simple solution without recursion


#1

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