vector<int> preorderTraversal(TreeNode *root) {
vector<int> res;
if (!root){
return res;
}
stack<TreeNode*> nodes;
nodes.push(root);
while (!nodes.empty()){
TreeNode* cur = nodes.top();
nodes.pop();
res.push_back(cur->val);
if (cur->right){
nodes.push(cur->right);
}
if (cur->left){
nodes.push(cur->left);
}
}
return res;
}