#include<iostream>
#include<vector>
#include<stack>
using namespace std;
using vecIter = std::vector<int>::iterator;
struct TreeLinkNode {
int val;
TreeLinkNode *left, *right, *next;
TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
};
class Solution
{
public:
Solution(){};
~ Solution(){};
void connect(TreeLinkNode* root){
if(NULL==root) return;
TreeLinkNode* curLev;
while(root->left!=NULL){
curLev=root;
while(curLev!=NULL){
curLev->left->next=curLev->right;
if(curLev->next!=NULL)
curLev->right->next=curLev->next->left;
curLev=curLev->next;
}
root=root->left;
}
}
};
TreeLinkNode* createBinaryTree(vecIter beg,vecIter end)
{
vector<TreeLinkNode*> vec;
for(vecIter it=beg;it!=end;++it)
vec.push_back(new TreeLinkNode(*it));
for(int i=0,pos=0;pos!=vec.size()-1;++i)
{
vec[i]->left=vec[++pos];
vec[i]->right=vec[++pos];
}
return *vec.begin();
}
void print(TreeLinkNode* root)
{
while(root)
{
cout<<root->val;
TreeLinkNode* cur=root->next;
while(cur)
{
cout<<"->"<<cur->val;
cur=cur->next;
}
cout<<endl;
root=root->left;
}
}
int main(int argc,char** argv){
vector<int> vec={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
TreeLinkNode* root=createBinaryTree(vec.begin(),vec.end());
Solution s;
s.connect(root);
print(root);
return 0;
}