我刚写了一遍,没有test
class node {
node *l;
node *r;
node *s;
};
void linksibling(node *root)
{
if (!root) return;
root->s = NULL;
node *c = root, *n, *h;
while (c)
{
// 找下一层的头
while (c && !c->l && !c->r)
c = c->s;
if (!c)
break;
if (c->l)
{
n = c->l;
h = n;
}
if (c->r)
{
if (n)
{
n->s = c->r;
n = c->r;
}
else {
n = c->r;
h = n;
}
}
// 链接下一层
while (c->s)
{
c = c->s;
if (c->l)
{
n->s = c->l;
n = n->s;
}
if (c->r)
{
n->s = c->r;
n = n->s;
}
}
n->s = NULL;
//
c = h;
}
}