Formatted question description:

116. Populating Next Right Pointers in Each Node

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.

    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,
       /  \
      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




Because it is a complete binary tree, if the left child node of the node exists, its right child node must exist, so the next pointer of the left child node can directly point to its right child node.

The processing method for its right child node is to check whether the next of its parent node is empty, if it is not empty, it points to the left child node of the node pointed to by its next pointer, if it is empty, it points to NULL.


Need to use queue to assist, because it is a bfs, the nodes of each layer are added to the queue in order, and whenever an element is taken from the queue, its next pointer can be pointed to the next node in the queue.

For each level, before starting the traversal of the elements at the beginning of the layer, first count the total number of the layer, and use a for loop, so that when the for loop ends, the layer has been traversed.



import java.util.LinkedList;
import java.util.Queue;

public class 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; }
	 * }

    // just a bfs
    public class Solution_iteration {
        public void connect(TreeLinkNode root) {

            if (root == null) {

            Queue<TreeLinkNode> q = new LinkedList<>();

            TreeLinkNode levelEndMarker = new TreeLinkNode(0);
            TreeLinkNode prev = new TreeLinkNode(0);


            while (!q.isEmpty()) {
                TreeLinkNode current = q.poll();

                if (current == levelEndMarker) {
                    // push marker again as next level end
                    if (!q.isEmpty()) { // @note: missed final check again...

                    // first node of each level, can not be last of upper level
                    prev = levelEndMarker; // can be any node which is not in this tree

                if (current.left != null) {
                if (current.right != null) {

       = current;
                prev = current;

    public class Solution_recursion {
        public void connect(TreeLinkNode root) {
            connect(root, null);

        public void connect(TreeLinkNode current, TreeLinkNode next) {
            if (current == null) {
            } else {
       = next; // connect

            connect(current.left, current.right);

            if (next != null) {
                connect(current.right, next.left);
            } else {
	            /*next == null*/
                connect(current.right, null);


All Problems

All Solutions