class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
stack<TreeNode*> stk;
//根节点非空 或 栈非空时
while (root != nullptr || !stk.empty()) {
//根非空 遍历左子树 当最左一个子节点时 跳出循环
while (root != nullptr) {
stk.push(root);
root = root->left;
}
//栈顶赋值给根节点
root = stk.top();
//最左子节点出栈
stk.pop();
//获取最左点的值
res.push_back(root->val);
//右子树 给根
root = root->right;
}
return res;
}
};