LeetCode | Populating Next Right Pointers in Each Node II
2014-03-10 00:01
190 查看
题目
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,
After calling your function, the tree should look like:
分析
用层次遍历的方法可以比较直观的解决这题(解法1)。
由于题目给出了next指针这个辅助空间,我们可以借助这个指针省去层次遍历里的队列,从而实现O(1)空间复杂度的解法(解法2)。
解法1
import java.util.LinkedList;
import java.util.Queue;
public class PopulatingNextRightPointersInEachNodeII {
public void connect(TreeLinkNode root) {
if (root == null) {
return;
}
Queue<TreeLinkNode> queue = new LinkedList<TreeLinkNode>();
Queue<TreeLinkNode> nextQueue = new LinkedList<TreeLinkNode>();
queue.add(root);
TreeLinkNode left = null;
while (!queue.isEmpty()) {
TreeLinkNode node = queue.poll();
if (left != null) {
left.next = node;
}
left = node;
if (node.left != null) {
nextQueue.add(node.left);
}
if (node.right != null) {
nextQueue.add(node.right);
}
if (queue.isEmpty()) {
Queue<TreeLinkNode> temp = queue;
queue = nextQueue;
nextQueue = temp;
left = null;
}
}
}
}解法2
public class PopulatingNextRightPointersInEachNodeII {
public void connect(TreeLinkNode root) {
if (root == null) {
return;
}
TreeLinkNode nextHead = null;
TreeLinkNode p = root;
TreeLinkNode pre = null;
while (p != null) {
if (p.left != null) {
if (pre == null) {
pre = p.left;
nextHead = pre;
} else {
pre.next = p.left;
pre = pre.next;
}
}
if (p.right != null) {
if (pre == null) {
pre = p.right;
nextHead = pre;
} else {
pre.next = p.right;
pre = pre.next;
}
}
p = p.next;
if (p == null) {
p = nextHead;
nextHead = null;
pre = null;
}
}
}
}
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
分析
用层次遍历的方法可以比较直观的解决这题(解法1)。
由于题目给出了next指针这个辅助空间,我们可以借助这个指针省去层次遍历里的队列,从而实现O(1)空间复杂度的解法(解法2)。
解法1
import java.util.LinkedList;
import java.util.Queue;
public class PopulatingNextRightPointersInEachNodeII {
public void connect(TreeLinkNode root) {
if (root == null) {
return;
}
Queue<TreeLinkNode> queue = new LinkedList<TreeLinkNode>();
Queue<TreeLinkNode> nextQueue = new LinkedList<TreeLinkNode>();
queue.add(root);
TreeLinkNode left = null;
while (!queue.isEmpty()) {
TreeLinkNode node = queue.poll();
if (left != null) {
left.next = node;
}
left = node;
if (node.left != null) {
nextQueue.add(node.left);
}
if (node.right != null) {
nextQueue.add(node.right);
}
if (queue.isEmpty()) {
Queue<TreeLinkNode> temp = queue;
queue = nextQueue;
nextQueue = temp;
left = null;
}
}
}
}解法2
public class PopulatingNextRightPointersInEachNodeII {
public void connect(TreeLinkNode root) {
if (root == null) {
return;
}
TreeLinkNode nextHead = null;
TreeLinkNode p = root;
TreeLinkNode pre = null;
while (p != null) {
if (p.left != null) {
if (pre == null) {
pre = p.left;
nextHead = pre;
} else {
pre.next = p.left;
pre = pre.next;
}
}
if (p.right != null) {
if (pre == null) {
pre = p.right;
nextHead = pre;
} else {
pre.next = p.right;
pre = pre.next;
}
}
p = p.next;
if (p == null) {
p = nextHead;
nextHead = null;
pre = null;
}
}
}
}
相关文章推荐
- LeetCode 117 Populating Next Right Pointers in Each Node II (链表 层次遍历 推荐)
- 【leetcode】Populating Next Right Pointers in Each Node II
- Leetcode 之Populating Next Right Pointers in Each Node II(51)
- [leetcode]Populating Next Right Pointers in Each Node II
- 【leetcode】Populating Next Right Pointers in Each Node I & II
- [LeetCode] Populating Next Right Pointers in Each Node II
- [LeetCode] Populating Next Right Pointers in Each Node II
- LeetCode刷题笔录Populating Next Right Pointers in Each Node II
- Leetcode Populating Next Right Pointers in Each Node II
- [leetcode] Populating Next Right Pointers in Each Node II
- [LeetCode]题解(python):117-Populating Next Right Pointers in Each Node II
- [LeetCode#117]Populating Next Right Pointers in Each Node II
- leetcode第一刷_Populating Next Right Pointers in Each Node II
- LeetCode 35 Populating Next Right Pointers in Each Node II
- leetcode之Populating Next Right Pointers in Each Node II
- LeetCode "Populating Next Right Pointers in Each Node II"
- leetcode - Populating Next Right Pointers in Each Node II
- [LeetCode]Populating Next Right Pointers in Each Node II
- LeetCode - 116/117 - Populating Next Right Pointers in Each NodeII
- [leetcode]Populating Next Right Pointers in Each Node II