Memory limit exceeded. Why?


  • 0
    A
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        vector<int> postorderTraversal(TreeNode* root) {
            TreeNode *p;
            stack<TreeNode*> s;
            vector<int> v;
            p=root;
            while(p!=NULL)
            {
                if(p->left!=NULL || p->right!=NULL)
                {
                    s.push(p);
                    if(p->left)
                        p=p->left;
                    else
                        p=p->right;
                }
                else if(p->left==NULL && p->right==NULL )
                {
                    v.push_back(p->val);
                    if(!s.empty())
                    { p=s.top();
                        if(p->right==NULL)
                            {
                                v.push_back(p->val);
                                s.pop();
                            }
                        else
                            p=p->right;
                    }
                    else
                        break;
                }
            }
            return v;
        }
    };

Log in to reply
 

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