Time Limit Exceeded Always


  • 0
    V
     * Definition for binary tree with next pointer.
     * struct TreeLinkNode {
     *  int val;
     *  TreeLinkNode *left, *right, *next;
     *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
     * };
     */
    class Solution {
    public:
        void connect(TreeLinkNode *root) {
            queue<pair<TreeLinkNode*,int> > Q;
    
            if (root == NULL)
                return ;
                
            Q.push(make_pair(root,0));
            int curr=-1 ;
            int count=1;
            pair < TreeLinkNode*, int > prev_node;
            while(!Q.empty()){
                pair < TreeLinkNode*, int > c_node = Q.front();
                Q.pop();
        
                if (c_node.first->right != NULL){
                    Q.push(make_pair(c_node.first->right,curr+1));
                    cout<<"Pushing right child of "<<c_node.first->val<<" "<<c_node.first->right->val<<endl;
                }
                if(c_node.first->left != NULL){
                    Q.push(make_pair(c_node.first->left,curr+1));
                    cout<<"Pushing right left of "<<c_node.first->val<<" "<<c_node.first->left->val <<endl;
                }   
                
                if(c_node.second > curr)
                    curr = c_node.second;
                else{
                    // cout<<c_node.second<<" "<<curr<<" "<<(c_node.first)->val<<endl;
                    cout<<prev_node.first->val<<" <-- "<<c_node.first->val<<endl;
                    c_node.first->right = prev_node.first;
                    // cout<<root->left->right->val<<endl;
                }
                
                prev_node = c_node;
            }
        }
    };
    

    This is what my solution is. I have checked it on my system using custom test cases. It works fine on my system, but The OJ still always shows me TLE. Can anyone help with this? i cant understand why


Log in to reply
 

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