class Solution { public: vectorpreorderTraversal(TreeNode *root) { vector res; stack s; TreeNode * p = root; while(p!=NULL||!s.empty()) { while(p!=NULL) { res.push_back(p->val); s.push(p); p = p->left; } if(!s.empty()) { p = s.top(); s.pop(); p = p->right; } } return res; } };