Sunday, June 12, 2011

A binary tree problem - Populating next right pointers in each node

Source

void connect(Node* p) {
if (p == NULL)
return;
if (p->leftChild == NULL || p->rightChild == NULL)
return;
Node* rightSibling;
Node* p1 = p;
while (p1) {
if (p1->nextRight)
rightSibling = p1->nextRight->leftChild;
else
rightSibling = NULL;
p1->leftChild->nextRight = p1->rightChild;
p1->rightChild->nextRight = rightSibling;
p1 = p1->nextRight;
}
connect(p->leftChild);
}

No comments:

Post a Comment