要求:迭代
思路:找到最左下才开始左中右,每个节点都要左中右,往左时入栈。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> v;
stack<TreeNode*> s;
TreeNode* cur=root;
while(cur||!s.empty()){
if(cur){
s.push(cur);
cur=cur->left;
}
else{
cur=s.top();
v.push_back(cur->val);
s.pop();
cur=cur->right;
}
}
return v;
}
};