6 line C++ none-recursion solution


  • 2
    W
    void connect(TreeLinkNode *root) {
        for ( ; root; root = root->left)
            for (auto cur = root; cur->left; cur = cur->next) {
                cur->left->next = cur->right;
                if (!cur->next) break;
                cur->right->next = cur->next->left;
            }
    }

  • 0
    A

    Such an elegant solution, thank you!


Log in to reply
 

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