http://oj.leetcode.com/problems/binary-tree-inorder-traversal/
/**
* Definition for binary tree
* 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> res;
// Start typing your C/C++ solution below
// DO NOT write int main() function
stack<TreeNode*> myStack;
TreeNode* current = root;
while(current!=NULL) {
myStack.push(current);
current=current->left;
}
while(myStack.empty()==false){
current = myStack.top();
myStack.pop();
res.push_back(current->val);
current=current->right;
while(current!=NULL) {
myStack.push(current);
current=current->left;
}
}
return res;
}
};
本文详细介绍了如何使用迭代法实现二叉树的中序遍历过程,通过栈的数据结构来辅助遍历操作,确保遍历顺序为左节点 -> 根节点 -> 右节点。
1770

被折叠的 条评论
为什么被折叠?



