Leetcode[116, 117] Populating Next Right Pointers in Each Node
08 Nov 2015###Task1 Given a binary tree
struct TreeLinkNode {
TreeLinkNode *left;
TreeLinkNode *right;
TreeLinkNode *next;
} Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
Initially, all next pointers are set to NULL.
Note:
You may only use constant extra space. You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children). For example, Given the following perfect binary tree, 1 / \ 2 3 / \ / \ 4 5 6 7 After calling your function, the tree should look like: 1 -> NULL / \ 2 -> 3 -> NULL / \ / \ 4->5->6->7 -> NULL
###Python ####Iterative
# Definition for binary tree with next pointer.
# class TreeLinkNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
# self.next = None
class Solution(object):
def connect(self, root):
"""
:type root: TreeLinkNode
:rtype: nothing
"""
if not root:
return
queue = collections.deque()
queue.append(root)
while len(queue) > 0:
size = len(queue)
prev = None
for i in range(size):
cur = queue.popleft()
if cur.left:
queue.append(cur.left)
if cur.right:
queue.append(cur.right)
if i == 0:
prev = cur
else:
prev.next = cur
prev = cur
####Recursive
# Definition for binary tree with next pointer.
# class TreeLinkNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
# self.next = None
class Solution(object):
def connect(self, root):
"""
:type root: TreeLinkNode
:rtype: nothing
"""
if not root:
return
if not root.left and not root.right:
return
root.left.next = root.right
if root.next and root.right:
root.right.next = root.next.left
self.connect(root.left)
self.connect(root.right)
###Java
/**
* 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;
}
LinkedList<TreeLinkNode> queue = new LinkedList<TreeLinkNode>();
queue.offer(root);
int numsLeft = 1;
int crtLevelNums = 0;
while (!queue.isEmpty()) {
TreeLinkNode crt = queue.poll();
numsLeft--;
if (numsLeft == 0) {
crt.next = null;
} else {
TreeLinkNode peeked = queue.peek();
crt.next = peeked;
}
if (crt.left != null) {
queue.offer(crt.left);
crtLevelNums++;
}
if (crt.right != null) {
queue.offer(crt.right);
crtLevelNums++;
}
if (numsLeft == 0) {
numsLeft = crtLevelNums;
crtLevelNums = 0;
}
}
return;
}
}
###Task2 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?
###Python ####Iterative
# Definition for binary tree with next pointer.
# class TreeLinkNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
# self.next = None
class Solution(object):
def connect(self, root):
"""
:type root: TreeLinkNode
:rtype: nothing
"""
if not root:
return
queue = collections.deque()
queue.append(root)
while len(queue) > 0:
size = len(queue)
prev = None
for i in range(size):
cur = queue.popleft()
if cur.left:
queue.append(cur.left)
if cur.right:
queue.append(cur.right)
if i == 0:
prev = cur
else:
prev.next = cur
prev = cur
####Recursive
# Definition for binary tree with next pointer.
# class TreeLinkNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
# self.next = None
class Solution(object):
def connect(self, root):
"""
:type root: TreeLinkNode
:rtype: nothing
"""
if not root:
return
if not root.left and not root.right:
return
if root.left and root.right:
root.left.next = root.right
rootN = self.findNext(root.next)
if root.right:
root.right.next = rootN
else:
root.left.next = rootN
self.connect(root.right)
self.connect(root.left)
def findNext(self, root):
if not root:
return None
if not root.left and not root.right:
return self.findNext(root.next)
if root.left:
return root.left
if root.right:
return root.right
###Java
/**
* 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;
}
LinkedList<TreeLinkNode> queue = new LinkedList<TreeLinkNode>();
queue.offer(root);
int numsLeft = 1;
int crtLevelNums = 0;
while (!queue.isEmpty()) {
TreeLinkNode crt = queue.poll();
numsLeft--;
if (numsLeft == 0) {
crt.next = null;
} else {
TreeLinkNode peeked = queue.peek();
crt.next = peeked;
}
if (crt.left != null) {
queue.offer(crt.left);
crtLevelNums++;
}
if (crt.right != null) {
queue.offer(crt.right);
crtLevelNums++;
}
if (numsLeft == 0) {
numsLeft = crtLevelNums;
crtLevelNums = 0;
}
}
return;
}
}
###Points
- O(n)
- Divide and conquer
- In case the tree is not complete, changes needed for recurive method: 1. Make sure node exists before point next pointer 2. Extra method find the next node in parent’s sibling; 3. Recursively call root.right first then root.left; otherwise the extra method cannot find the correct next node