Memory Exceed C++ Solution.


  • 0
    S
    class Solution {
    public:
        std::vector<int> postorderTraversal(TreeNode *root) {
            std::vector<int> result;
            TreeNode *node = root;
            TreeNode *lastVisited;
            std::stack<TreeNode*> s;
            while (!s.empty() || node) {
                if (node) {
                    s.push(node);
                    node = node->left;
                } else {
                    TreeNode *peak = s.top();
                    if (peak->right && lastVisited != peak->right) {
                        node = peak->right;
                    } else {
                        result.push_back(peak->val);
                        s.pop();
                    }
                }
            }
            return result;
        }
    };
    

    This is kinda the same with wikipedia. I have no idea why it is memory exceed. Runs ok when I test it locally. Does anybody has any ideas? Thanks in advance.


  • 0
    L

    You forgot to change the value of lastVisited my darling.


  • 0
    S

    My bad. Thank you so much.


Log in to reply
 

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