A concise O(1) space complexity solution
it fits problem 1 and 2, any comments will be welcome, thanks\n\n    void connect(TreeLinkNode root) {\n        TreeLinkNode head = root; // the left first node
it fits problem 1 and 2, any comments will be welcome, thanks\n\n    void connect(TreeLinkNode *root) {\n        TreeLinkNode *head = root; // the left first node in every level\n        TreeLinkNode *cur = NULL;  // the current node in the upper level\n        TreeLinkNode *pre = NULL;  // the prev node in the downer level\n        \n        while (head) {\n            cur = head;\n            head = pre = NULL;\n            // travel one level in a loop\n            while (cur) {\n                // left child exist\n                if (cur->left) {\n                    if (pre) pre = pre->next = cur->left;\n                    else head = pre = cur->left;\n                    \n                }\n                // right child exist\n                if (cur->right) {\n                    if (pre) pre = pre->next = cur->right;\n                    else head = pre = cur->right;\n                }\n                // next node in the same level\n                cur = cur->next;\n            }\n        }\n    }
[]