Leetcode 之Binary Tree Inorder Traversal(43)
树的中序遍历。先不断压入左结点至末尾,再访问,再压入右结点。注意和先序遍历的比较
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
vector<int> inorderTraversal(TreeNode *root) { vector<int> result; stack<TreeNode *>s; TreeNode *p = root; while (!s.empty() || p != nullptr) { if (p != nullptr) { //将当前结点和其左结点不断入栈 s.push(p); p = p->left; } else { //访问到了左结点末尾 //访问该结点并弹出 p = s.top(); result.push_back(p->val); s.pop(); //将方向转为其右结点 p = p->right; } } return result; }