Binary Tree Postorder Traversal Runtime Error


  • 0
    T

    I need help understanding why im getting runtime error on Binary Tree Postorder Traversal. Is there anything i forgot to do?

    class Solution {
    public:
        vector<int> postorderTraversal(TreeNode* root) 
        {
            vector<int> res;
            if(!root)
                return res;
            vector<pair<TreeNode* , bool> > stk;
            stk.push_back({root , false});
            while(stk.size())
            {
                if(!stk.back().first)
                    stk.pop_back();
                else if(stk.back().second) {
                    res.push_back(stk.back().first->val);
                    stk.pop_back();
                } else {
                    auto &pr = stk.back();
                    stk.push_back({pr.first->right , false});
                    stk.push_back({pr.first->left, false});
                    pr.second = true;
                }
            }
            return res;
        }
    };
    

Log in to reply
 

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