Output Limit Exceeded


  • 0
    D

    have not use recursion.test case has passed.don't know where is wrong.

    class Solution {
    public:
        vector<vector<int>> ans1;
        vector<int> path;
        int ans;
        vector<vector<int>> pathSum(TreeNode *root, int sum) {
            stack<TreeNode *> s;
            stack<int> ss;
            ans=0;
            TreeNode *p=root,*q;
            while(p||!s.empty()){
                while(p){
                    ans+=p->val;
                    path.push_back(p->val);
                    if(!p->left&&!p->right&&ans==sum){
                        ans1.push_back(path);
                    }
                    s.push(p);
                    ss.push(ans);
                    p=p->left;
                }
                if(!s.empty()){
                    p=s.top();s.pop();
                    while(ans!=ss.top()){
                        ans-=path.back();
                        path.pop_back();
                    }
                    ss.pop();
                    p=p->right;
                }
            }
            return ans1;
        }
    };

  • 0
    S

    My advice. using post order traversal to figure out the path sum, and then figure out this problem using the same solution.


  • 0
    D
    This post is deleted!

Log in to reply
 

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