您的位置:首页 > Web前端 > Node.js

【second】Populating Next Right Pointers in Each Node

2013-10-23 14:47 246 查看
void connect(TreeLinkNode *root) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(!root)
return;
TreeLinkNode* first,*cur;
first = cur = root;
while(cur)
{
if(cur->left)
{
cur->left->next = cur->right;
if(cur->next)
cur->right->next = cur->next->left;
}else
return;

cur = cur->next;
if(cur==NULL)
{
cur = first->left;
first = cur;
}
}

}


  其实本质上是tree的level-order遍历

void connect(TreeLinkNode *root) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(!root)
return;
TreeLinkNode* first,*cur;
first =  root;
while(first)
{
cur = first;
while(cur)
{
if(cur->left)
{
cur->left->next = cur->right;
if(cur->next)
cur->right->next = cur->next->left;
}else
return;
cur = cur->next;
}
first = first->left;
}

}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: