[LeetCode] [LeetCode] Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node".
What if the given tree could be any binary tree? Would your previous solution still work?
Note:
- You may only use constant extra space.
For example,
Given the following binary tree,
1
/ \
2 3
/ \ \
4 5 7
After calling your function, the tree should look like:
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ \
4-> 5 -> 7 -> NULL
/**
* 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:
TreeLinkNode *NextNode(TreeLinkNode *p) {
while (p) {
if (p->left)
return p->left;
if (p->right)
return p->right;
p = p->next;
} return NULL;
} void connect(TreeLinkNode *root) {
if (root == NULL) return;
TreeLinkNode *level_begin = root;
while (level_begin) {
TreeLinkNode *cur = level_begin;
while (cur) {
if (cur->left)
cur->left->next = (cur->right != NULL) ? cur->right : NextNode(cur->next);
if (cur->right)
cur->right->next = NextNode(cur->next);
cur = cur->next;
}
level_begin = NextNode(level_begin); //下一层的开始节点
}
}
};
思路二:
一个prev记录当前层前一节点是啥(用来连接的
一个next记录下一层的开始(用户切换到下一层)
/**
* 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) {
while(root) {
TreeLinkNode *next = NULL; //the first node of next level
TreeLinkNode *prev = NULL; //previous node on the same level
for (; root; root = root->next) {
if (!next) next = root->left ? root->left : root->right; if (root->left) {
if (prev) prev->next = root->left;
prev = root->left;
}
if (root->right) {
if (prev) prev->next = root->right;
prev = root->right;
}
} root = next;
}
}
};
[LeetCode] [LeetCode] 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 ...
随机推荐
- 实验四: Android程序设计
实验四 Android程序设计 1 实验目的及要求 1.安装 Android Stuidio. 2.完成Hello World, 要求修改res目录中的内容,Hello World后要显示自己的学号. ...
- 20155216 2016-2017-2 《Java程序设计》第七周学习总结
20155216 2016-2017-2 <Java程序设计>第七周学习总结 教材学习内容总结 认识Lambda表达式语法 Lambda表达式不需要也不允许使用throws关键字来声明可能 ...
- 20155339 2016-2017-2 《Java程序设计》第3周学习总结
20155339 2016-2017-2 <Java程序设计>第3周学习总结 教材学习内容总结 第四章 认识对象 类与对象 1.定义类时使用class关键词再加名称,例如 class Cl ...
- 【CF833E】Caramel Clouds
[CF833E]Caramel Clouds 题面 洛谷 题目大意: 天上有\(n\)朵云,每朵云\(i\)会在时间\([li,ri]\)出现,你有\(C\)个糖果,你可以花费\(c_i\)个糖果让云 ...
- Apache入门篇(三)之apache2.4.33的新特性解析与虚拟主机实战
1.http 2.4新特性 新特性: (1) 在编译时可以将多个MPM构建为可加载模块,可以在运行时通过LoadModule指令配置所选的MPM: (2) 2.2版本的event MPM在实验阶段,到 ...
- Arduino 101/Genuino101使用-第2篇
1. Arduino 101编程只是在ARC的核心上进行,其具体架构为ARCv2EM.. 2. 而Quark核心,从目前可知的信息来看,其应该运行着名为Zephyr的RTOS 3.101并没有EEPR ...
- 搜索引擎ElasticSearch系列(四): ElasticSearch2.4.4 sql插件安装
一:ElasticSearch sql插件简介 With this plugin you can query elasticsearch using familiar SQL syntax. You ...
- Linux 安装Redis<准备>(使用Mac远程访问)
阅读本文需要一定的Linux基础 一 Redis简介 redis是用c语言编写的一款开源的高性能键值对(key-value)数据库 它通过提供多种键值数据类型来适应不同场景下的存储需求 二 Redis ...
- tendermint 跟tikv结合
import ( "fmt" "github.com/allegro/bigcache" "github.com/kooksee/usmint/cmn ...
- linux下搭建python机器学习环境
前言 在 linux 下搭建 python 机器学习环境还是比较容易的,考虑到包依赖的问题,最好建立一个虚拟环境作为机器学习工作环境,在建立的虚拟环境中,再安装各种需要的包,主要有以下6个(这是看这个 ...