What`s wrong with my solution. The status show output limits exceeded. But I can`t figure out the wrong place.


  • 0
    L
    class Solution {
    

    public:
    void connect(TreeLinkNode *root) {
    vector<TreeLinkNode *> nodeArry;
    queue <TreeLinkNode *> que;
    if(root == NULL)
    return;
    que.push(root);
    que.push(NULL);

    while(true)
    {
        TreeLinkNode *cur = que.front();
        que.pop();
        if(cur != NULL)
        {
            nodeArry.push_back(cur);
            if(cur->left)
            que.push(cur->left);
            if(cur->right)
            que.push(cur->right);
        }
        else
        {
            if(que.empty())
            {
                nodeArry.push_back(NULL);
                break;
            }
          
            que.push(NULL);
        }
    }
    for(int i = 0; i < nodeArry.size()-1; i++)
    {
        if(nodeArry[i] != NULL)
        nodeArry[i]->next = nodeArry[i + 1];
    }
    

    }
    };


Log in to reply
 

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