/**
 * 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) {
            return;
        }
        
        root.left.next = root.right;
        if (root.next == null) {
            root.right.next = null;
        } else {
            root.right.next = root.next.left;
        }
        
        connect(root.right);
        connect(root.left);
    }
}

@@ -339,17 +339,17 @@
 ot.left 
-=
+!
 = null) 
@@ -367,186 +367,92 @@
    r
-eturn;%0A        %7D%0A        %0A        root.left.next = root.right;%0A        if (root.next == null) %7B%0A            root.right.next = null;%0A        %7D else %7B%0A            root.right.next =
+oot.left.next = root.right;%0A            root.right.next = root.next == null ? null :
  roo

Update PopulatingNextRightPointersInEachNode.java