void connect(TreeLinkNode *root)
{
while (root)
{
//每一层循环时重新初始化
TreeLinkNode *prev = nullptr;
TreeLinkNode *next = nullptr;
//对于每一层
for (; root; root = root->next)
{
//每一层开始时,记录下一层的起始结点
if (!next)next = root->left ? root->left : root->right; if (root->left)
{
//如果不是起始结点,则将prev与该左子结点相连接
if (prev)prev->next = root->left;
//如果是每层的起始结点,则将左子结点直接赋给prev
prev = root->left;
}
if (root->right)
{
if (prev)prev->next = root->right;
prev = root->right;
}
}
root = next;
}
}

Leetcode 之Populating Next Right Pointers in Each Node II(51)的更多相关文章

  1. [Leetcode Week15]Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...

  2. [LeetCode] 117. Populating Next Right Pointers in Each Node II 每个节点的右向指针 II

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  3. 【leetcode】Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...

  4. Leetcode 树 Populating Next Right Pointers in Each Node II

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II ...

  5. Java for LeetCode 117 Populating Next Right Pointers in Each Node II

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  6. [Leetcode][JAVA] Populating Next Right Pointers in Each Node II

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  7. leetcode 117 Populating Next Right Pointers in Each Node II ----- java

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  8. Leetcode#117 Populating Next Right Pointers in Each Node II

    原题地址 二叉树的层次遍历. 对于每一层,依次把各节点连起来即可. 代码: void connect(TreeLinkNode *root) { if (!root) return; queue< ...

  9. Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II

    题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...

随机推荐

  1. 模态窗口插件之Jbox

    $.jBox.tip("报损数量不能大于库存数!", 'error'); $.jBox.tip("请选择要报损的产品", "warn"); ...

  2. Source Tree for MAC1.6

    Atlassian ID has become the new Atlassian Account. Read more about it here. After some great communi ...

  3. poppin_xpower_ 常城

  4. <supports-screens>的用法

    <supports-screens android:resizeable=["true"| "false"] android:smallScreens=[ ...

  5. No message found under code ' for locale 'en'.

    1.如果你使用eclipse创建的工程是class和src分开的,那么资源属性文件一定要放在src目录以内.2.属性文件名的写法:messages_zh_CN.properties (中文)messa ...

  6. Sqlserver 读取EXCEL

    1.1 启用本地读取设置 --启用EXEC sp_configure 'show advanced options', 1RECONFIGUREEXEC sp_configure 'Ad Hoc Di ...

  7. BZOJ1208 宠物收养所

    Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...

  8. BZOJ3626 LCA

    Description 给出一个n个节点的有根树(编号为0到n-1,根节点为0).一个点的深度定义为这个节点到根的距离+1. 设dep[i]表示点i的深度,LCA(i,j)表示i与j的最近公共祖先. ...

  9. 破解受保护的excel中的密码

    今天朋友给我发了一张excel表,她说不能删除数据,让我帮忙弄弄,我当时就觉得这张表应该是受到保护了,就在视图选项中准备撤销保护,但是却需要密码,后来在网上查找了 下资料,发现有个方法可以将密码破解出 ...

  10. CentOS 配置hadoop

    Hadoop是用作处理大数据用的,核心是HDFS.Map/Reduce.虽然目前工作中不需要使用这个,但是,技多不压身,经过虚拟机很多遍的尝试,终于将Hadoop2.5.2的环境顺利搭建起来了.    ...