/**
* 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> inorderTraversal(TreeNode* root) {
vector<int> ans;
stack<TreeNode* > nodeStack;
TreeNode* current = root;
if (root == NULL)
return ans;
while (nodeStack.size() || current != NULL)
{
while (current != NULL)
{
nodeStack.push(current);
current = current->left;
}
if (!nodeStack.empty())
{
current = nodeStack.top();
nodeStack.pop();
ans.push_back(current->val);
current = current->right;
}
}
return ans;
}
};