题目:

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. 为oracle中的表格增加列和删除列

    http://blog.csdn.net/rainharder/article/details/6663458 alter table 表名 drop column 列名eg:alter table ...

  2. HTML5之缓存

    ----- 缓存文件 - 使用UTF-8编码- 以Cache Manifest 开头- 三个基本部分 CACHE MANIFESTmenu.htmlmenu.js# login requires ne ...

  3. rpm与yum命令的初步认识

    RPM:Red Hat package manager(RedHat软件包管理工具),现在为RPM is Package Manager好比windows里的文件扩展名为·exe的软件包. RPM的包 ...

  4. 【C#】添加鼠标管轮事件

    对FlowLayoutPanel添加鼠标滚轮事件 在mainform中添加事件 his.flowLayoutPanel1.MouseWheel += new System.Windows.Forms. ...

  5. Excel数据复制到Winform控件ListView

    先给窗体添加一个右键菜单contextMenuStrip 加一个下拉项[粘贴] 粘贴事件: private void tsmiPaste_Click(object sender, EventArgs ...

  6. SpecFlow教程--快速入门

    原文http://www.specflow.org/getting-started/ 一.安装 为了能正确安装SpecFlow所需要的东西,你必须安装集成IDE的插件以及设置你的项目使用SpecFlo ...

  7. MySQL基础学习之函数

    数学函数 绝对值      abs() 圆周率      PI() 平方根 sqrt() 模除取余   mod(被除数,除数) 随机数      rand() 四舍五入    round(数字) 次方 ...

  8. Pandas之容易让人混淆的行选择和列选择

    在刚学Pandas时,行选择和列选择非常容易混淆,在这里进行一下讨论和归纳 本文的数据来源:https://github.com/fivethirtyeight/data/tree/master/fa ...

  9. Beaglebone Back学习一(开发板介绍)

    随着开源软件的盛行.成熟,开源硬件也迎来了春天,先有Arduino,后有Raspherry Pi,到当前的Beaglebone .相信在不久的将来,开源项目将越来越多,越来越走向成熟.         ...

  10. Ducci Sequence

    Description   A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers ( ...