populate next right pointer in each node

来源:互联网 发布:中原g7 知乎 编辑:程序博客网 时间:2024/06/05 19:15

void populate(TreeLinkNode* root)

{

while(root)

{

TreeLinkNode* pre=nullptr;

TreeLinkNode* next=nullptr;

for(;root;root=root->next)

{

if(!next)

next=root->left?root->left:root->right;

if(root->left!=nullptr)

{

if(pre)pre->next=root->left;

pre=root->left;

}

if(root->right!=nullptr)

{

if(pre)pre->next=root->right;

pre=root->right;

}

}

root=next;

}

0 0