117. 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
链接: http://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/
题解:
一样是DFS。跟上题不同在于,给定输入不是完全二叉树了,所以要加入一些条件来判断是否存在一个合理的next值。并且对左节点和右节点的有效性也要验证。最后要先递归连接右节点,再connect左节点。
Time Complexity - O(n), Space Complexity - O(1)。
public class Solution {
public void connect(TreeLinkNode root) {
if(root == null)
return;
TreeLinkNode node = root.next;
while(node != null){
if(node.left != null){
node = node.left;
break;
} else if(node.right != null){
node = node.right;
break;
}
node = node.next;
}
if(root.right != null){
root.right.next = node;
if(root.left != null)
root.left.next = root.right;
} else {
if(root.left != null)
root.left.next = node;
}
connect(root.right);
connect(root.left);
}
}
Update:
/**
* Definition for binary tree with next pointer.
* public class TreeLinkNode {
* int val;
* TreeLinkNode left, right, next;
* TreeLinkNode(int x) { val = x; }
* }
*/
public class Solution {
public void connect(TreeLinkNode root) {
if(root == null)
return;
TreeLinkNode p = root.next; while(p != null) {
if(p.left != null) {
p = p.left;
break;
} else if (p.right != null) {
p = p.right;
break;
}
p = p.next;
} if(root.right != null)
root.right.next = p;
if(root.left != null)
root.left.next = (root.right != null) ? root.right : p; connect(root.right);
connect(root.left);
}
}
题外话: 刚看完crimson peak,还不错。不过小小失望是本来以为是个恐怖片,观众小朋友们买好了可乐和爆米花准备挑战一下自己,结果是个离奇曲折婉转动人的凄美爱情片...我只想说,导演你太浪漫了, 我先刷两题压压惊 -______-!!
二刷:
依然使用了递归,并没有做到constant space。留给三刷了。
Java:
Time Complexity - O(n), Space Complexity - O(n)。
/**
* Definition for binary tree with next pointer.
* public class TreeLinkNode {
* int val;
* TreeLinkNode left, right, next;
* TreeLinkNode(int x) { val = x; }
* }
*/
public class Solution {
public void connect(TreeLinkNode root) {
if (root == null) return;
TreeLinkNode nextNode = root.next;
while (nextNode != null) {
if (nextNode.left == null && nextNode.right == null) nextNode = nextNode.next;
else break;
}
if (nextNode != null) nextNode = (nextNode.left != null) ? nextNode.left : nextNode.right;
if (root.right != null) root.right.next = nextNode;
if (root.left != null) root.left.next = (root.right != null) ? root.right : nextNode;
connect(root.right);
connect(root.left);
}
}
iterative:
Level order traversal。主要就是类似二叉树层序遍历。这回把顶层看作一个linkedlist,我们只需要继续连接这linkedlist中每个节点的子节点们。当顶层遍历完毕以后,下一层正好也形成了一个新的类linkedlist。我们换到下一层以后继续遍历,直到最后。
Java:
Time Complexity - O(n), Space Complexity - O(1)。
/**
* Definition for binary tree with next pointer.
* public class TreeLinkNode {
* int val;
* TreeLinkNode left, right, next;
* TreeLinkNode(int x) { val = x; }
* }
*/
public class Solution {
public void connect(TreeLinkNode root) {
TreeLinkNode curLevel = new TreeLinkNode(-1);
TreeLinkNode newLevel = curLevel;
while (root != null) {
if (root.left != null) {
curLevel.next = root.left;
curLevel = curLevel.next;
}
if (root.right != null) {
curLevel.next = root.right;
curLevel = curLevel.next;
}
root = root.next;
if (root == null) {
curLevel = newLevel;
root = newLevel.next;
newLevel.next = null;
}
}
}
}
Update:
/**
* Definition for binary tree with next pointer.
* public class TreeLinkNode {
* int val;
* TreeLinkNode left, right, next;
* TreeLinkNode(int x) { val = x; }
* }
*/
public class Solution {
public void connect(TreeLinkNode root) {
if (root == null) return;
TreeLinkNode curLevel = new TreeLinkNode(-1);
TreeLinkNode newLevel = curLevel;
while (root != null) {
if (root.left != null) {
curLevel.next = root.left;
curLevel = curLevel.next;
}
if (root.right != null) {
curLevel.next = root.right;
curLevel = curLevel.next;
}
root = root.next;
if (root == null) {
root = newLevel.next;
newLevel.next = null;
curLevel = newLevel;
}
} }
}
Reference:
http://www.cnblogs.com/springfor/p/3889327.html
https://leetcode.com/discuss/67291/java-solution-with-constant-space
https://leetcode.com/discuss/60795/o-1-space-o-n-time-java-solution
https://leetcode.com/discuss/24398/simple-solution-using-constant-space
https://leetcode.com/discuss/65526/ac-python-o-1-space-solution-12-lines-and-easy-to-understand
https://leetcode.com/discuss/3339/o-1-space-o-n-complexity-iterative-solution
117. Populating Next Right Pointers in Each Node II的更多相关文章
- leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II
leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)
[LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 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 ...
- 【LeetCode】117. Populating Next Right Pointers in Each Node II (2 solutions)
Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...
- 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】#117. Populating Next Right Pointers in Each Node II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- 117. Populating Next Right Pointers in Each Node II (Tree; WFS)
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- 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] 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 ...
随机推荐
- c#学习汇总-----------------多态
刚开通了博客园的博客,原因是我的师兄和前辈们在这里写的一些学习笔记让我受益匪浅,所以决定从今天起用这个平台来记录我的点滴学习心得.我喜欢GIS二次开发,以后应该也不会脱离于此,对于编程我积累的知识太零 ...
- C++多线程技术windows常用方法
随着计算机CPU计算能力快速提高,计算机的处理性能和并行性能力也大大提升.那么,一味使用运行时标准库的C++语言也应该开始支持多线程技术.今天,我为大家带来了C++在windows平台下的常用多线程方 ...
- Linux ---> 简单socket
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> ...
- Xamarin android PreferenceActivity 实现应用程序首选项设置(一)
应用程序首选项屏幕 类似系统设置界面. PreferenceActivity 是另一种类型的Activity,通过PreferenceActivity 可以以最少量的工作显示某些Preference列 ...
- Linq to sql 接收存储过程返回的多个结果集
故事前提.......... 一.返回顺序结果集 存储过程实例 CREATE PROCEDURE MultipleResultTypesSequentially AS select * from pr ...
- DTCMS中文章增加tags标签和关键词时中文,替换为英文状态,
DTCMS.Web\admin\article\article_edit.aspx 找到添加和修改的方法 model.tags = txtTags.Text.Trim()model.seo_keywo ...
- CocoaPods安装和使用及问题:Setting up CocoaPods master repo
CocoaPods是什么? 当你开发iOS应用时,会经常使用到很多第三方开源类库,比如JSONKit,AFNetWorking等等.可能某个类库又用到其他类库,所以要使用它,必须得另外下载其他类库,而 ...
- 模仿 ios 分段单选
http://blog.csdn.net/qduningning/article/details/37935227 res/drawable/seg_left.xml <?xml version ...
- PHP中CURL技术模拟登陆抓取网站信息,用与微信公众平台成绩查询
伴随微信的红火,微信公众平台成为许多开发者的下一个目标.笔者本身对于这种新鲜事物没有如此多的吸引力.但是最近有朋友帮忙开发微信公众平台中一个成绩查询的功能.于是便在空余时间研究了一番. 主要的实现步骤 ...
- Catalyst揭秘 Day7 SQL转为RDD的具体实现
Catalyst揭秘 Day7 SQL转为RDD的具体实现 从技术角度,越底层和硬件偶尔越高,可动弹的空间越小,而越高层,可动用的智慧是更多.Catalyst就是个高层的智慧. Catalyst已经逐 ...