【second】Populating Next Right Pointers in Each Node
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;
}
}
浙公网安备 33010602011771号