写到一半,被导师叫到办公室说了一个多小时的事情,回来继续写,居然还AC也是人品爆发。
class Solution {
public:
void connect(TreeLinkNode *root) {
//递归算法实现,由于是满二叉树,左子树和右子树是对称的,可以将左子树和右子树从根节点开始连接,类似判断对称二叉树的算法
if(!root||(root&&!root->left&&!root->right))
return;
else
connect(root->left,root->right);
}
void connect(TreeLinkNode *rl,TreeLinkNode *rr)
{
rl->next=rr;
if(!rl->right)//如果是叶子结点,就直接返回
return;
connect(rl->right,rr->left);//左根节点和右根节点的第二层连接
connect(rl->left,rl->right);//左子树内部的连接
connect(rr->left,rr->right);//右子树的连接
}
};