Simple Iterative solution
Populate the levels one by one. curLevel points to node whose children will be linked, nextLevel points to the first node in the next level.\n\n    class Soluti
Populate the levels one by one. curLevel points to node whose children will be linked, nextLevel points to the first node in the next level.\n\n    class Solution {\n    public:\n        void connect(TreeLinkNode *root) {\n            if(!root)return;\n            TreeLinkNode*curLevel=root,*nextLevel=root->left;\n            root->next=NULL;\n            while(curLevel->left){\n                curLevel->left->next=curLevel->right;\n                if (curLevel->next){\n                    curLevel->right->next = curLevel->next->left;\n                    curLevel=curLevel->next;\n                }\n                else {\n                    curLevel->right->next=NULL;\n                    curLevel=nextLevel;\n                    nextLevel=nextLevel->left;\n                }\n            }\n        }\n    };
[]