原题地址

二叉树的层次遍历。

对于每一层,依次把各节点连起来即可。

代码:

 void connect(TreeLinkNode *root) {
if (!root) return; queue<TreeLinkNode *> parents; parents.push(root);
while (!parents.empty()) {
queue<TreeLinkNode *> children; while (!parents.empty()) {
TreeLinkNode *p = parents.front();
parents.pop();
p->next = parents.empty() ? NULL : parents.front();
if (p->left)
children.push(p->left);
if (p->right)
children.push(p->right);
}
parents = children;
}
}

Leetcode#117 Populating Next Right Pointers in Each Node II的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 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 ...

  4. leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II

    leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  5. 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)

    [LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  6. 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 ...

  7. 【LeetCode】117. Populating Next Right Pointers in Each Node II (2 solutions)

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

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

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

  9. 【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 ...

随机推荐

  1. 整理一些有意思的php笔试题

    慢慢补充 1.下面这段代码的输出是什么: $a = in_array('01', array('1'))==var_dump('01'==1); echo $a; 说明:in_array('01', ...

  2. Hbase负载均衡流程以及源码

    hmater负责把region均匀到各个region server .hmaster中有一个线程任务是专门处理负责均衡的,默认每隔5分钟执行一次. 每次负载均衡操作可以分为两步: 生成负载均衡计划表 ...

  3. python 解析XML python模块xml.dom解析xml实例代码

    分享下python中使用模块xml.dom解析xml文件的实例代码,学习下python解析xml文件的方法. 原文转自:http://www.jbxue.com/article/16587.html ...

  4. nginx报file not found错误

    查看错误日志 [:q2013/10/20 18:41:40 [error] 27151#0: *106117 FastCGI sent in stderr: "Primary script ...

  5. MySQL下查看用户和建立用户

    启动数据库: [root@server ~]# mysqld_safe & [1] 3289 [root@server ~]# 130913 08:19:58 mysqld_safe Logg ...

  6. STM32F0xx_DAC输出电压配置详细过程

    前言 数模转换DAC的功能在现实应用中所占的分量,相对定时器TIM.串口USART等要小的多,这也是ST为什么内部集成DAC模块相对来说不是那么多的原因.但在有需要使用数模转换功能的项目中,自带的这个 ...

  7. java的变量

    什么是变量? 在计算机中用来存储信息,通过声明语句来指明存储位置和所需空间. 变量的声明方法及赋值 分号:语句结束标志             赋值号:将=右边的值赋给左边的变量 变量有哪些数据类型? ...

  8. linux内核设计与实现学习笔记-模块

    模块 1.概念:  如果让LINUX Kernel单独运行在一个保护区域,那么LINUX Kernel就成为了“单内核”.    LINUX Kernel是组件模式的,所谓组件模式是指:LINUX K ...

  9. wordpress nginx 开启链接为静态

    使用固定连接里的自定义 /%postname%/ 日志标题的缩略版本(日志/页面编辑界面上的日志别名).因此“This Is A Great Post!”在URI中会变成this-is-a-great ...

  10. [转]unzip解压windows zip乱码的处理

    [转]unzip解压windows zip乱码的处理 http://blog.sina.com.cn/s/blog_6c9d65a101012gz0.html 朋友从windows传过来的zip文件, ...