Just like "starting from enumeration and check pattern" strategy, a good solution starts from basics. classSolution {public:voidconnect(TreeLinkNode *p) {if(!p)return;if(p->left) p->left->next = p->right;//with the same parentif(p->right) p->right->next = (p->next) ? p->next->left : NULL; connect(p->left); ...
Compared with I version, the tree could be incomplete. The only difference is that, we connect current node's child to next non-childrenless' node's first child. Still, we need calculate from right to left: classSolution {public:voidconnect(TreeLinkNode *p) {if(!p)return;if(p->right)...