/**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
class Solution {
public:
void connect(TreeLinkNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
connectCore(root, NULL);
}
void connectCore(TreeLinkNode *ptr, TreeLinkNode *pNext)
{
if (ptr != NULL)
{
ptr->next = pNext;
connectCore(ptr->left, ptr->right);
connectCore(ptr->right, pNext != NULL ? pNext->left : NULL);
}
}
};