Share my 11 ms C solution


  • 0

    more solutions see:https://github.com/lightmen/leetcode.git

    void connect(struct TreeLinkNode *root) {

    struct TreeLinkNode *cur,*parent,*first,*first_child;
    cur = root;
    while(cur){
        parent = cur;
        first = NULL;
        while(parent){
            first_child = NULL;
            if(parent->left){
                first_child = parent->left;
            }else if(parent->right){
                first_child = parent->right;
            }
            if(first && first_child){
                first->next = first_child;
                first = first->next;
            }else if(!first){
                first = first_child;
                cur = first; // first TreeLinkNode is first node of the next loop
            }
            if(parent->left && parent->right){
                first->next = parent->right;
                first = first->next;
            }
            parent = parent->next;
        }
    }
    

    }


Log in to reply
 

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