/**
* 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> preorder;
vector<int> inorder;
stack<TreeNode *> s;
TreeNode *cur = root;
while (cur != NULL || !s.empty()) {
while (cur != NULL) {
s.push(cur);
preorder.emplace_back(cur->val);//这里输出是前序遍历
cur = cur->left;
}
TreeNode *top = s.top();
s.pop();
inorder.emplace_back(top->val);//这里输出是中序遍历
cur = top->right;
}
return v;
}
};