Binary Tree Postorder Traversal Memory Limit Exceeded


  • 0
    M
    class Solution {
    public:
            vector<int> postorderTraversal(TreeNode *root) {
            vector<int> result;
            stack<TreeNode *> rev;
            if(!root) return result;
            rev.push(root);
            while(!rev.empty()){
                TreeNode * temp=rev.top();
                if(temp->right){
                    rev.push(temp->right);
                    
                    }
                if(temp->left){
                    rev.push(temp->left);
                    
                }
                if(temp->left==NULL && temp->right==NULL){
                    result.push_back(temp->val);
                    temp=NULL;
                    rev.pop();
                }
                temp=NULL;
                
            }
            
            return result;
        }
        
    };
    

    why memory limit problem? could anyone help to find the bug?


Log in to reply
 

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