Why i am getting wrong answer


#1

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