Why this Solution is wrong? anybody can help me!


  • 0
    0
    void connect(TreeLinkNode *root) 
    {
        if (!root) return;
        TreeLinkNode* node1 = root->left;
        TreeLinkNode* node2 = root->right;
        while (node1 && node2)
        {
            node1->next = node2;
            if (node1->right) node1 = node1->right;
            else node1= node1->left;
            
            if (node2->left) node2 = node2->left;
            else node2 = node2->right;
        }
        connect(root->left);
        connect(root->right);
    }

  • 0
    0

    I have known the reason, in this case i was wrong

                              1
                           /     \
                         2        3
                       /    \         \
                     4     5         6
                   /                     \
                 7                       8

Log in to reply
 

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