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 ...
随机推荐
- 用scala的actor并发编程写一个单机版的WorldCount
前言:最近一段时间比较忙,也是比较懒了吧,好长时间没写博客了,新的一年到来,给自己一个小目标,博客坚持写下去,分享一下这历程!废话不多说,开始正题咯(希望大家喜欢!) 首先这算是一个scala程序的入 ...
- 面试前赶紧看了5道Python Web面试题,Python面试题No17
目录 本面试题题库,由公号:非本科程序员 整理发布 第1题: Flask中的请求上下文和应用上下文是什么? 第2题:django中间件的使用? 第3题: django开发中数据做过什么优化? 第4题: ...
- Python的集合与字典练习
集合与字典练习 question1 问题描述:有一个列表,其中包括 10 个元素,例如这个列表是[1,2,3,4,5,6,7,8,9,0],要求将列表中的每个元素一次向前移动一个位置,第一个元素到列表 ...
- Tufurama CodeForces - 961E
Tufurama CodeForces - 961E 题意:有一部电视剧有n季,每一季有ai集.问有多少对i,j存在第i季第j集也同时存在第j季第i集. 思路:核心问题还是统计对于第i季,你要统计第i ...
- poj 3045 叠罗汉问题 贪心算法
题意:将n头牛叠起来,每头牛的力气 s体重 w 倒下的风险是身上的牛的体重的和减去s 求最稳的罗汉倒下去风险的最大值 思路: 将s+w最大的放在下面,从上往下看 解决问题的代码: #include& ...
- BZOJ 4393: [Usaco2015 Dec]Fruit Feast
DP #include<cstdio> using namespace std; int T,A,B,F[5000005],G[5000005]; int main(){ scanf(&q ...
- 【Gitlab+Jenkins+Ansible】构建自动化部署
说明: Gitlab.Jenkins.生产服务器.测试服务器上都需要安装Git. 一.安装Gitlab 1.主机配置 IP: 10.10.10.105 OS: CentOs7. Gitlab版本:gi ...
- SXCPC2018 nucoj1999 占领城市
#include <iostream> #include <cstring> #include <cstdio> #include <queue> us ...
- 史上最全的MSSQL笔记
http://www.cnblogs.com/gameworld/archive/2015/09/08/4790881.html
- 设计模式之第20章-访问者模式(Java实现)
设计模式之第20章-访问者模式(Java实现) “嘿,你脸好红啊.”“精神焕发.”“怎么又黄了?”“怕冷,涂的,涂的,蜡.”“身上还有酒味,露馅了吧,原来是喝酒喝的啊.”“嘿嘿,让,让你发现了,今天来 ...