Any one can help me find the error?


  • 0
    Q
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> res;
        if(!root)
            return res;
        stack<TreeNode* > s;
        TreeNode* temp = root;
        s.push(root);
        TreeNode* pre;
        while(!s.empty())
        {
            while(temp->left)
            {
                temp = temp->left;
                s.push(temp);
            }
            temp=s.top();
            if(temp->right && pre != temp->right)
            {
                temp = temp->right;
                s.push(temp);
            }
            else
            {
                res.push_back(temp->val);
                pre = temp;
                s.pop();
            }
        }
        return res;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.