CPP代码解法如下:
class Solution {
public:
vector
vector
stack
TreeNode* cur = root;//指针
while (cur != NULL || !st.empty()) {
if (cur != NULL) {
st.push(cur);
cur = cur->left;
}
else {
cur = st.top();
st.pop();
result.push_back(cur->val);
cur = cur->right;
}
}
return result;
}
};
下面将以二叉树模拟代码执行过程
此二叉树的正确中序遍历结果为:4 2 5 1 6 3 7
模拟元素进入和出去栈ST的过程:
就没有然后了,模拟过程之后题目就能看懂了!



