my 28ms C++ solution


  • 0
    D

    void connect(TreeLinkNode *root) {
    if (root == 0)
    return;

        vector<TreeLinkNode*> nodeVec;
        nodeVec.push_back(root);
        
        int start = 0;
        while(start < nodeVec.size())
        {
            int size = nodeVec.size();
            while(start < size)
            {
                if (start == size -1)
                    nodeVec[start]->next = 0;
                else
                    nodeVec[start]->next = nodeVec[start + 1];
                
                if (nodeVec[start]->left)
                    nodeVec.push_back(nodeVec[start]->left);
                    
                if (nodeVec[start]->right)
                    nodeVec.push_back(nodeVec[start]->right);
                
                ++start;
            }
        }
    }

Log in to reply
 

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