/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> res;
if(root==NULL) return res;
stack<TreeNode*> sta;
sta.push(root);
while(!sta.empty()){
TreeNode* tmp=sta.top();
res.push_back(tmp->val);
sta.pop();
if(tmp->right!=NULL){
sta.push(tmp->right);
}
if(tmp->left != NULL){
sta.push(tmp->left);
}
}
return res;
}
};