题目:

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. Libcurl笔记三

    一,post请求和回报处理 //"host/path?extra" //strHttp=" http://portal.liuhan.com:/web/getConfig ...

  2. 一个疑惑的的问题-ntvdm.exe进程

    今天测试反馈了一个问题,在启动我们程序某个模块的程序时,会立即出现一个ntvdm.exe进程,此进程会占用大量的系统资源,导致系统卡住. 当第一眼看到这个现象时,以为是电脑中毒了,所以立即在网上查. ...

  3. linux ubuntu 思源黑体安装

    下载地址: 全部:700多M  https://github.com/adobe-fonts/source-han-sans/releases/tag/1.001R 可选部分Github : http ...

  4. sql server 查看创建视图的sql语句

    --sp_helptext v_viewnamesp_helptext port_dept--效果

  5. Spark Tungsten揭秘 Day4 内存和CPU优化使用

    Spark Tungsten揭秘 Day4 内存和CPU优化使用 今天聚焦于内存和CPU的优化使用,这是Spark2.0提供的关于执行时的非常大的优化部分. 对过去的代码研究,我们会发现,抽象的提高, ...

  6. Spark Streaming揭秘 Day5 初步贯通源码

    Spark Streaming揭秘 Day5 初步贯通源码 引子 今天,让我们从Spark Streaming最重要的三个环节出发,让我们通过走读,逐步贯通源码,还记得Day1提到的三个谜团么,让我们 ...

  7. SQL优化的四个方面,缓存,表结构,索引,SQL语句

    一,缓存 数据库属于 IO 密集型的应用程序,其主要职责就是数据的管理及存储工作.而我们知道,从内存中读取一个数据库的时间是微秒级别,而从一块普通硬盘上读取一个IO是在毫秒级别,二者相差3个数量级.所 ...

  8. Oracle中SAVEPOINT和ROLLBACK用法

    savepoint是事务内部允许部分rollback的标志符.因为事务中对记录做了修改,我们可以在事务中创建savepoint来标识不同的点.如果遇到错误,就可以rollback到不同的点或直接回来事 ...

  9. Mysql grant权限管理

    MySQL 赋予用户权限命令的简单格式可概括为: grant 权限 on 数据库对象 to 用户 [identified by '密码'] 最常用的,弄主从同步的时候,给从库的slave用户设置拥有所 ...

  10. SQL学习中(一)序列

    序列可以理解数值序列生成器,通俗的说是按照已经设定的规则自动产生数据的方案对象.--SQL SERVER不支持 个人认为序列类似于SQLSERVER中的identity(1,1),可以用于在表中添加数 ...