[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 ...
随机推荐
- FlexPaper 里的pdf2json.exe 下载地址
在使用FlexPaper 做在线阅读,需要使用到pdf2json.exe,将PDF转成JSON或者XML格式,网上很少下载的,现在提供一个下载的地址 http://pan.baidu.com/s/1i ...
- React with webpack
//entry.js require("!style!css!./style.css"); require("./hello.jsx"); // documen ...
- OpenStack入门篇(一)之云计算的概念
1.云计算 云计算是一种按使用量付费的模式,这种模式提供可用的.便捷的.按需的网络访问, 进入可配置的计算资源共享池(资源包括网络,服务器,存储,应用软件,服务),这些资源能够被快速提供,只需投入很少 ...
- maven 发布springboot项目
1.把Spring Boot打包成JAR的形式,需要在pom.xml文件对应以下代码 <build> <finalName>ljl</finalName> //打包 ...
- 通过java反射机制获取该类的所有属性类型、值
转自:http://blog.csdn.net/sd4000784/article/details/7448221 方法使用了这俩个包下的 field 和method import Java.lang ...
- Drupal8 新建第一个模块
参考: https://www.drupal.org/developing/modules/8 https://www.drupal.org/node/1915030 https://www.drup ...
- 如何在存储过程的IN操作中传递字符串变量
原始SQL如下: SELECT MONTH(OrderTime) AS datetype, SUM(DeliveryCount) AS decount, Region FROM (SELECT dbo ...
- json_encode替代函数
<?php function jsonEncode($var) { if (function_exists('json_encode')) { return json ...
- Ubuntu用户设置文件说明
Ubuntu用户设置文件说明 Ubuntu作为Linux的一个发行版本,自然具有Linux系统的多用户特性.因为经常会使用和管理Ubuntu的用户,现将Ubuntu系统下的User的个性化配置整理如下 ...
- win7重装系统后设置Python2.7环境
起因 台式机的主板莫名出现问题,显示器画面卡顿不能动,鼠标键盘无反应,在这种情况下只好按住电源键断电.下面重启后,显示器无画面,猜测开机后没有进BIOS.然后就拆机箱,拔下电源线后撬起主板电池几秒再放 ...