/**
* 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
if(!root)
return;
root->next=NULL;
dfs(root);
}
void dfs(TreeLinkNode* p){
if(!p)
return;
if(p->left)
p->left->next=p->right;
if(p->right)
p->right->next=p->next?p->next->left:NULL;
dfs(p->left);
dfs(p->right);
}
};