LeetCode OJ-- Populating Next Right Pointers in Each Node II **@
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 **@的更多相关文章
- [Leetcode Week15]Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...
- [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 ...
- 【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 ...
- Leetcode 树 Populating Next Right Pointers in Each Node II
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II ...
- 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 ...
- [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 ...
- 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 ...
- Leetcode 之Populating Next Right Pointers in Each Node II(51)
void connect(TreeLinkNode *root) { while (root) { //每一层循环时重新初始化 TreeLinkNode *prev = nullptr; TreeLi ...
- Leetcode#117 Populating Next Right Pointers in Each Node II
原题地址 二叉树的层次遍历. 对于每一层,依次把各节点连起来即可. 代码: void connect(TreeLinkNode *root) { if (!root) return; queue< ...
- 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 ...
随机推荐
- javaWeb开发中常见的问题
1.修改表单提交的时候不好使可能是因为没写对应隐藏域的ID 2.el表达式在js代码中要加“”,例如 "${}" 3.JavaScript中的函数也有重载的特性.如果两个input ...
- 【104】Maven3.5.0结合eclipse使用,提示Lambda expressions are allowed only at source level 1.8 or above错误的解决方法
错误重现 我的机器上安装了 maven 3.5.0,在 eclipse 中创建 maven 项目.pom.xml配置如下: <project xmlns="http://maven.a ...
- python列表中的赋值与深浅拷贝
首先创建一个列表 a=[[1,2,3],4,5,6] 一.赋值 a=[[1,2,3],4,5,6]b=aa[0][1]='tom'print(a)print(b)结果: [[1, 'tom', 3], ...
- Java8_Lambda表达式
从2014年java8发布到现在已经有几个年头了,现在java11都发布了.公司最近把服务器环境重新搭建了一遍,jdk版本也从7换成了8,终于可以在代码里面写Lambda表达式了.作为 ...
- 做一个日收入100元的APP!
[导语]虽然讲了很多个人开发者的文章,但新手开发者如何赚自己的第一个100块钱,确是最难的事情.群里有人说都不知道干什么 app赚钱,完全没有想法, 并且经常问我有什么快速赚钱的方法.我只能遗憾地说, ...
- Monkey官方帮助翻译&介绍
都说想学好就看原文,英文不好为了翻译这个可费了大劲了.表格从GOOGLE官网复制到WORD里编辑,结果贴上来格式全乱了,只得不弄表格了.表格中官网的事件不是最新的,比最新的少2个,具体我会另发一篇文章 ...
- 安装的 Python 版本太多互相干扰?pyenv 建议了解一下。
写在之前 我们都知道现在的 Python 有 Python2 和 Python3,但是由于各种乱七八糟的原因导致这俩哥们要长期共存,荣辱与共,尴尬的是这哥俩的差异还比较大,在很多时候我们可能要同时用到 ...
- [oldboy-django][6其他]navicat远程登录没有权限
day6-17-1204 # 增加远程访问mysql的权限(就是其他ip地址远程访问另外一个ip地址的数据库) -- step1 修改配置文件,bind_address, 允许所有ip地址都可以访问m ...
- input框的限制(仅数字以及电话号码的限制)
电话号码限制 <input type="text" id="phone" maxlength="11" onkeyup=" ...
- poj 3278 catch that cow BFS(基础水)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 61826 Accepted: 19329 ...