117. Populating Next Right Pointers in Each Node II

Problem:

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:

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

Solution:

ONE

Recursive. See 116. Populating Next Right Pointers in Each Node.

The tree may not be perfect now. So keep finding next until there is a node with children, or null.

This also means post-order traversal is required.

/**
 * Definition for binary tree with next pointer.
 * function TreeLinkNode(val) {
 *     this.val = val;
 *     this.left = this.right = this.next = null;
 * }
 */

/**
 * @param {TreeLinkNode} root
 * @return {void} Do not return anything, modify tree in-place instead.
 */
var connect = function(root) {
  if (!root) { return }
  let next = null
  for (let node = root.next; node !== null; node = node.next) {
    if (node.left !== null) {
      next = node.left
      break
    }
    if (node.right !== null) {
      next = node.right
      break
    }
  }
  if (root.right !== null) {
    root.right.next = next
  }
  if (root.left !== null) {
    root.left.next = root.right || next
  }
  connect(root.right)
  connect(root.left)
};

TWO

Level order traversal. Exact same as 116. Populating Next Right Pointers in Each Node.

/**
 * Definition for binary tree with next pointer.
 * function TreeLinkNode(val) {
 *     this.val = val;
 *     this.left = this.right = this.next = null;
 * }
 */

/**
 * @param {TreeLinkNode} root
 * @return {void} Do not return anything, modify tree in-place instead.
 */
var connect = function(root) {
  if (!root) { return }

  const queue = [NaN, root]
  while (queue.length > 1) {
    const node = queue.shift()
    if (node !== node) {
      for (let i = 0; i < queue.length; i++) {
        queue[i].next = queue[i+1] || null
      }
      queue.push(NaN)
    } else {
      if (node.left !== null) { queue.push(node.left) }
      if (node.right !== null) { queue.push(node.right) }
    }
  }
};

Template generated via Leetmark.