Populating Next Right Pointers in Each Node II

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 toNULL.

Initially, all next pointers are set toNULL.

Note:

  • You may only use constant extra space.
  • Recursive approach is fine, implicit stack space does not count as extra space for this problem.

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

Analysis

A very concise and clear level-order traversal (@davidtan1890):

public class Solution {
    public void connect(TreeLinkNode root) {

        while(root != null){
            TreeLinkNode tempChild = new TreeLinkNode(0);
            TreeLinkNode currentChild = tempChild;
            while(root!=null){
                if(root.left != null) { 
                    currentChild.next = root.left; 
                    currentChild = currentChild.next;
                }
                if(root.right != null) { 
                    currentChild.next = root.right; 
                    currentChild = currentChild.next;
                }
                root = root.next;
            }
            root = tempChild.next;
        }
    }
}

Iterative

用一个 dummy node nextHead来代表指向下一行的开头节点,并用一个tail指针初始化为nextHead作为下一行进行连接的指针,当前行则用curr表示,当curr.left或者curr.right不为null,就将tail.next指向它们,并且继续移动tail = tail.next

一行遍历结束,也就是curr = null,就要重新赋值nextHead.nextcurr,因为nextHead.next指向的是下一行的第一个node;

这里要注意nextHead.next = null;因为如果不舍为空,那么循环就无法结束。

Solution

Iterative - O(n) time, O(1) space

public class Solution {
    public void connect(TreeLinkNode root) {
        TreeLinkNode nextHead = new TreeLinkNode(0); // dummy node for next level
        nextHead.next = root;
        TreeLinkNode tail = null;
        TreeLinkNode curr = null;
        while (nextHead.next != null) {
            tail = nextHead;
            curr = nextHead.next;
            nextHead.next = null;
            while (curr != null) {
                if (curr.left != null) {
                    tail.next = curr.left;
                    tail = tail.next;
                }
                if (curr.right != null) {
                    tail.next = curr.right;
                    tail = tail.next;
                }
                curr = curr.next;
            }
        }
    }
}

Recursive

public class Solution {
    public void connect(TreeLinkNode root) {
        if (root == null)
            return;
        TreeLinkNode dummy = new TreeLinkNode(-1);
        for (TreeLinkNode curr = root, prev = dummy; curr != null; curr = curr.next) {
            if (curr.left != null){
                prev.next = curr.left;
                prev = prev.next;
            }
            if (curr.right != null) {
                prev.next = curr.right;
                prev = prev.next;
            }
        }
        connect(dummy.next);
    }
}

Reference

https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/37828/O(1)-space-O(n)-complexity-Iterative-Solution

https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/37828/O(1)-space-O(n)-complexity-Iterative-Solution/35897

results matching ""

    No results matching ""