9-line fast c++ without recursion


  • 6
    M
    class Solution {
    public:
        void connect(TreeLinkNode *root) {
            while (root) {
                TreeLinkNode *a = root;
                while (a) {
                    if (a->left) {
                        a->left->next = a->right;
                        if (a->next)
                            a->right->next = a->next->left;
                    }
                    a=a->next;
                }
                root=root->left;
            }
        }
    };

Log in to reply
 

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