class Solution {
public:
vector<int> preorderTraversal(TreeNode* root)
{
stack<TreeNode*> s;
vector<int> v;
TreeNode* cur=root;
while(cur || !s.empty())
{
while(cur)
{
s.push(cur);
v.push_back(cur->val);
cur=cur->left;
}
TreeNode* top=s.top();
s.pop();
cur=top->right;
}
return v;
}
};
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
stack<TreeNode*> s;
vector<int> v;
TreeNode* cur=root;
while(cur || !s.empty())
{
while(cur)
{
s.push(cur);
cur=cur->left;
}
TreeNode* top=s.top();
s.pop();
v.push_back(top->val);
cur=top->right;
}
return v;
}
};
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
stack<TreeNode*> s;
vector<int> v;
TreeNode* cur=root,*prev=nullptr;
while(cur || !s.empty())
{
while(cur)
{
s.push(cur);
cur=cur->left;
}
TreeNode* top=s.top();
if(top->right==nullptr || top->right==prev)
{
s.pop();
v.push_back(top->val);
prev=top;
}
else
{
cur=top->right;
}
}
return v;
}
};