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. 如何使用开源库,吐在VS2013发布之前,顺便介绍下VS2013的新特性"Bootstrap"

    刚看到Visual Studio 2013 Preview - ASP.NET, MVC 5, Web API 2新功能搶先看 看了下VS2013带来的"新特性",直觉上看,除了引 ...

  2. css圆角矩形及去掉空格属性

    css圆角矩形 -webkit-border-radius:5px; -moz-border-radius:5px; border-radius:5px; 去掉空格 white-space:nowra ...

  3. Linux各目录作用

  4. 07.C#泛型的限制和可空类型的简单说明(三章3.5-四章4.1)

    自己在写文章的同时,也是在学习,对于书中的语句很多其实没有太好的理解,读一本书,要消化!!!三章都是讲泛型的,最后写一下泛型的限制,对于本章学习的完结,one end,one begin. 看下面的代 ...

  5. 一头扎进EasyUI3

    惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧! 一头扎进EasyUI第11讲 .基本下拉组件 <select id="cc" style=& ...

  6. angular的GitHub Repository Directive Example学习

    angular的GitHub Repository Directive Example学习 <!DOCTYPE html> <html ng-app="myApp" ...

  7. 每天一个linux命令(51):rcp命令

    rcp代表“remote file copy”(远程文件拷贝).该命令用于在计算机之间拷贝文件.rcp命令有两种格式.第一种格式用于文件到文件的拷贝:第二种格式用于把文件或目录拷贝到另一个目录中. 1 ...

  8. JWPlayer中字幕文件的配置

    最近应项目要求研究JWPlayer,视研究进度可能会将解决的问题或者一些配置方法写在这里. jwplayer支持vtt和srt格式的字幕文件,在视频中可以选择加载多个字幕文件(常用于多语言字幕),并且 ...

  9. PR 不能手动修改素材尺寸的解决方法

    选中素材,然后再特效控制台那边点击一下运动就可以在预览窗口直接用鼠标调整画面大小和位移了.

  10. 列表 list

    例子: #!/usr/bin/python li = list([11,22,33,66,99,77]) print(li) li.extend((44,55,)) print(li) ret = l ...