Idea is pretty similiar to Binary Tree Zigzag Level Order Traversal – Traverse through the tree using BFS, and memorize the node at each level in order. Then just link up the nodes.

    void connect(TreeLinkNode *root) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        vector<vector<TreeLinkNode*> > res;
        for(int i = 0 ; i < res.size(); i++){
            for(int j = 1 ; j < res[i].size() ; j++){
                res[i][j-1]->next = res[i][j];
    void traverse(TreeLinkNode *root, vector<vector<TreeLinkNode*> > &res, int lvl){
        if(root == NULL) return;
        while(res.size() <= lvl){
            vector<TreeLinkNode*> t;
        traverse(root->left, res, lvl+1);
        traverse(root->right, res, lvl+1);