Populating Next Right Pointers in Each Node 解答

Question

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

Solution 1 -- BFS

Key to the solution is to traverse tree level by level. Therefore, we can use BFS. Time complexity O(n), n is the number of nodes, and space cost is 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) {
// We can use BFS to solve this problem
List<TreeLinkNode> current = new ArrayList<TreeLinkNode>();
List<TreeLinkNode> next;
if (root == null)
return;
current.add(root);
while (current.size() > 0) {
next = new ArrayList<TreeLinkNode>();
int length = current.size();
for (int i = 0; i < length; i++) {
TreeLinkNode currentTreeNode = current.get(i);
if (i < length - 1)
currentTreeNode.next = current.get(i + 1);
else
currentTreeNode.next = null;
if (currentTreeNode.left != null)
next.add(currentTreeNode.left);
if (currentTreeNode.right != null)
next.add(currentTreeNode.right);
}
current = next;
}
}
}

Solution 2 -- Recursive

Consider for one node, the connection is done when:

1. Its left node (if exists) connect to its right node.

2. Its right node (if exists) connect to its next node's left child.

3. Its left child and right child are all connected.

Note the prerequisite for this problem is perfect binary tree (every parent has two children). Time complexity O(n), space cost 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) {
if (root == null)
return;
if (root.left != null)
root.left.next = root.right;
if (root.right != null && root.next != null)
root.right.next = root.next.left;
connect(root.left);
connect(root.right);
}
}

Solution 3 -- Four Pointers

We can use four pointers to traverse the tree.

(referrence: ProgramCreek)

Populating Next Right Pointers in Each Node 解答

 /**
* 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 lastHead = root;//prevous level's head
TreeLinkNode lastCurrent = null;//previous level's pointer
TreeLinkNode currentHead = null;//current level's head
TreeLinkNode current = null;//current level's pointer while (lastHead != null) {
lastCurrent = lastHead;
currentHead = lastHead.left;
current = lastCurrent.left;
while (lastCurrent != null && current != null) {
current.next = lastCurrent.right;
current = current.next;
lastCurrent = lastCurrent.next;
if (lastCurrent != null) {
current.next = lastCurrent.left;
current = current.next;
}
}
lastHead = currentHead;
currentHead = null;
}
}
}
上一篇:jsp网站环境搭建


下一篇:POJ - 2115C Looooops 扩展欧几里得(做的少了无法一眼看出)