二叉树中序遍历

二叉树非递归中序遍历

 

class Solution {
public:
    /**
     * @param root: A Tree
     * @return: Inorder in ArrayList which contains node values.
     */
    vector<int> inorderTraversal(TreeNode * root) {
        // write your code here
        
        stack<TreeNode*> stack;
        vector<int> res;
        
        
        TreeNode* curt = root;
        
        while(curt != nullptr || !stack.empty())
        {
            while(curt != nullptr)
            {
                stack.push(curt);
                curt = curt->left;
            }
            
            curt = stack.top();
            stack.pop();
            res.push_back(curt->val);
            curt = curt->right;
        }
        
        
        return res;
    }
};

  

 

posted @ 2019-07-16 16:14  unicoe  阅读(239)  评论(0编辑  收藏  举报