/**
* 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:
Node* connect(Node* root) {
pointNext(root);
return root;
}
private:
void pointNext(Node *root) {
if (!root) {
return;
}
if (root->left) {
root->left->next = root->right;
}
if (root->right && root->next) {
root->right->next = root->next->left;
}
pointNext(root->left);
pointNext(root->right);
}
};