题目:

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
Hide Tags

Tree Depth-first Search 

链接: 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的更多相关文章

  1. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  2. 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)

    [LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  3. 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 ...

  4. 【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 ...

  5. 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 ...

  6. 【一天一道LeetCode】#117. Populating Next Right Pointers in Each Node II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  7. 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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. Poj 2996 Help Me with the Game

    1.Link: http://poj.org/problem?id=2996 2.Content: Help Me with the Game Time Limit: 1000MS   Memory ...

  2. MySQ binlog三种模式

    MySQ binlog三种模式及设置方法 1.1 Row Level  行模式 日志中会记录每一行数据被修改的形式,然后在slave端再对相同的数据进行修改 优点:在row level模式下,bin- ...

  3. 3.IP转发

    1. "vim  /usr/lib/sysctl.d/00-system.conf"在#Disable netfilter on bridges.栏下面添加行:"net. ...

  4. 隐藏win7盘符

    1.隐藏盘符: //新建注册表,隐藏X盘符 int regeditme() { HKEY hkey; DWORD dwLastError= ;//隐藏X盘2^25 J:2^9=512 X:盘符与挂载的 ...

  5. ALI OSS RequestTimeTooSkewed

    php版阿里oss sdk,请求时抛RequestTimeTooSkewed错误,说时间差距太大,搜了一下发现是服务器的时间设置问题. 我们在安装完Centos Linux操作系统之后,点击系统的时间 ...

  6. Linux学习之路一计算机是如何工作的

    初次接触MOOC课堂,里面有个很牛X的老师教Linux,恰好自己有兴趣学,顾有了此系列学习博文. 第一讲   计算机是如何工作的 学习Linux,涉及到了C语言和汇编以及操作系统的知识,顾第一讲要讲讲 ...

  7. 解决flash挡住层的问题

    让div在flash上面 设置flash为透明: 插件代码换成如下: <object type="application/x-shockwave-flash" data=&q ...

  8. javascript常用对象

    A,window对象 window对象是浏览器模型对象的顶层对象 常用属性: screen:客户端的屏幕和显示性能的信息. history:客户端访问过的url信息 location:当前url链接的 ...

  9. C#定时器

    在C#里关于定时器类就有3个 1.定义在System.Windows.Forms里 2.定义在System.Threading.Timer类里 3.定义在System.Timers.Timer类里 S ...

  10. Node.js之【express 安装问题】

    经常在全局安装express后,在cmd里面会找不到express命令, 本地模式安装express:'express' 不是内部或外部命令,也不是可运行的程序或批处理文件. 1.先全局安装expre ...