https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/

接上一题目,输入的树不是perfect的。

在这里深搜就不好使了。因为,在你往下深搜的时候,可能上一行的next还没处理到那里,所以会丢失信息。

利用它的next结构,进行bfs,同时记录下一行的head节点。

/**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
class Solution {
public:
void connect(TreeLinkNode *root) {
if(root == NULL)
return; TreeLinkNode *NextRowHead = NULL;
TreeLinkNode *NextRowPrev = NULL;
while(root)
{
if(root->left)
{
if(NextRowHead == NULL) // the first element of next row
{
NextRowHead = root->left;
}
else
NextRowPrev->next = root->left; //它前面有点 NextRowPrev = root->left;
}
if(root->right)
{
if(NextRowHead == NULL)
{
NextRowHead = root->right;
}
else
NextRowPrev->next = root->right; NextRowPrev = root->right;
}
root = root->next; if(root == NULL) //begin a new row
{
root = NextRowHead;
NextRowHead = NULL;
NextRowPrev = NULL;
}
}
}
};

LeetCode OJ-- Populating Next Right Pointers in Each Node II **@的更多相关文章

  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 之Populating Next Right Pointers in Each Node II(51)

    void connect(TreeLinkNode *root) { while (root) { //每一层循环时重新初始化 TreeLinkNode *prev = nullptr; TreeLi ...

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

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

  10. 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. GIt-恢复进度

    继续暂存区未完成的实践 经过了前面的实践,现在DEMO版本库应该处于master分支上,看看是不是这样. $ cd /path/to/my/workspace/demo $ git status -s ...

  2. vue/vux编译时出现 unexpected token <11:0-485>

    最近开发Vux项目,一直使用VS Code开发工具,可以格式化里面的<script>代码的: 但是今天突然无法格式化代码,而且编译报错.主要提示类似:unexpected token  & ...

  3. 数组线性表ArrayList 和链表类LinkedList

    数组线性表类ArrayList 和链表类LinkedList 是实现List接口的两个具体类.ArrayList 数组储存元素,这个数组是动态创建的.如果元素个数超过了数组的容量,就创建一个更大的新数 ...

  4. [c++基础]3/5原则--拷贝构造函数+拷贝赋值操作符

    /* * main.cpp * * Created on: Apr 7, 2016 * Author: lizhen */ #include <iostream> #include &qu ...

  5. java流(二)

    目录 1 ObjectOutputStream/ObjectInputStream的使用 2 序列化 3 具体序列化的过程 4 Externalizable的简易介绍 实现序列化的Person类 /* ...

  6. OOP的三大特性------封装、继承、多态

    封装 1.<1>类背后隐藏的思想是数据抽象和封装 <2>信息隐藏,隐藏对象的实现细节,不让外部直接访问到 将数据成员和成员函数一起包装到一个单元里,单元以类的形式实现 < ...

  7. java 数据库连接 驱动相关参数

    mysql: driverClass=com.mysql.jdbc.Driver connectionURL=jdbc:mysql://localhost:3306/shiro userId=root ...

  8. Spring MVC请求参数绑定

    所谓请求参数绑定,就是在控制器方法中,将请求参数绑定到方法参数上 @RequestParam 绑定单个请求参数到方法参数上 @RequestParam("id") Integer ...

  9. ZOJ 3362 Beer Problem(SPFA费用流应用)

    Beer Problem Time Limit: 2 Seconds      Memory Limit: 32768 KB Everyone knows that World Finals of A ...

  10. [luoguP2224] [HNOI2001]产品加工(背包DP)

    传送门 f[i][j]表示第一个机器耗时j,第二个机器耗时f[i][j] 第一维可以滚掉 #include <cstdio> #include <cstring> #inclu ...