/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型vector
*/
//不使用递归的方式实现
vector<int> inorderTraversal(TreeNode* root) {
vector<int> ans;
process(root,ans);
return ans;
}
void process(TreeNode* root,vector<int>& ans){
if(root==nullptr){
return;
}
stack<TreeNode*> stack;
while(root!=nullptr||!stack.empty()){
if(root!=nullptr){
stack.push(root);
root=root->left;
}else{
root=stack.top();
stack.pop();
ans.push_back(root->val);
root=root->right;
}
}
}
};