http://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/
/**
* 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) {}
* };
*/
using namespace std;
class Solution {
public:
void connect(TreeLinkNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(root==NULL) return;
queue<TreeLinkNode*> nodes;
nodes.push(root);
nodes.push(NULL);
while(nodes.size()!=0){
TreeLinkNode* current=nodes.front();
nodes.pop();
if(current!=NULL){
current->next=nodes.front();
if(current->left!=NULL) nodes.push(current->left);
if(current->right!=NULL)nodes.push(current->right);
}
else{
if(nodes.size()!=0)
nodes.push(NULL);
}
}
}
};