/**
* 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>output;
// if(root==NULL)return output;
// helper(root,output);
TreeNode*cur=root;
stack<TreeNode*>ans;
while(!ans.empty()||cur!=NULL){
while(cur!=NULL){
ans.push(cur);
cur=cur->left;
}
cur=ans.top();
ans.pop();
output.push_back(cur->val);
cur=cur->right;
}
return output;
}
private:
void helper(TreeNode*root,vector<int>&res){
if(root==NULL)return;
helper(root->left,res);
res.push_back(root->val);
helper(root->right,res);
}
};
使用了递归和非递归方式