Time Limit Exceeded: Last executed input: [1]... Tree Postorder Traversal


  • 0
    G
    /**
     * Definition for binary tree
     * 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) {
            vector<int> res;
            TreeNode *tmp;
            stack<TreeNode*> stack_;
            stack_.push(root);
            if(!root) return res;
            while(root||!stack_.empty())
            {
                if(root)
                {
                    if(root->right) stack_.push(root->right);
                    if(root->left) stack_.push(root->left);
                    root = root->left;
                }
                else
                {
                    tmp = stack_.top();
                    res.push_back(tmp->val);
                    stack_.pop();
                    while(!stack_.empty() && stack_.top()->right == tmp)
                    {
                        tmp = stack_.top();
                        res.push_back(tmp->val);
                        stack_.pop();
                    }
                }
                if(!stack_.empty()) root = stack_.top();
            }
            
            return res;
        }
    };
    

    it run well no my computer,but
    Time Limit Exceeded:
    Last executed input: [1]

    i dont know why!!!!


Log in to reply
 

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