Feb 28, 2009

Given a binary tree link all the right child of a node to their left siblings if present.

Given a binary tree link all the right child of a node to their left siblings if present.
foo(tree* root)
{
if(!root) return;
stl::queue s;
s.push(root);
while(!s.empty())
{
tree* curr=s.front();
s.pop();
if(curr->left&&curr->right)
connect curr->left to curr->right;
if(curr->left)
s.push(curr->left);
if(curr->right)
s.push(curr->right);
}
return;
}

No comments:

Post a Comment