题目:html
Follow up for problem "Populating Next Right Pointers in Each Node".java
What if the given tree could be any binary tree? Would your previous solution still work?node
Note:python
For example,
Given the following binary tree,spring
1 / \ 2 3 / \ \ 4 5 7
After calling your function, the tree should look like:ide
1 -> NULL / \ 2 -> 3 -> NULL / \ \ 4-> 5 -> 7 -> NULL
Tree Depth-first Search
连接: http://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/spa
题解:code
同样是DFS。跟上题不一样在于,给定输入不是彻底二叉树了,因此要加入一些条件来判断是否存在一个合理的next值。而且对左节点和右节点的有效性也要验证。最后要先递归链接右节点,再connect左节点。htm
Time Complexity - O(n), Space Complexity - O(1)。blog
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