/**
 * 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.right == null) {
            TreeLinkNode tmp = root.next;
            this.pointTo(root.left, tmp);
        } else if (root.left == null && root.right != null) {
            TreeLinkNode tmp = root.next;
            this.pointTo(root.right, tmp);
        } else if (root.left != null && root.right != null) {
            root.left.next = root.right;
            TreeLinkNode tmp = root.next;
            this.pointTo(root.right, tmp);
        } else {
            return;
        }
        
        this.connect(root.right);
        this.connect(root.left);
    }
    
    public void pointTo(TreeLinkNode from, TreeLinkNode to) {
        while (to != null) {
            if (to.left != null) {
                from.next = to.left;
                break;
            }
            if (to.right != null) {
                from.next = to.right;
                break;
            }
            to = to.next;
        }
    }
}

@@ -299,32 +299,36 @@
 eturn;%0A        %7D
+    
 %0A        %0A      
@@ -354,157 +354,61 @@
 null
- && root.right == null) %7B%0A            TreeLinkNode tmp = root.next;%0A            this.pointTo(root.left, tmp);%0A        %7D else if (
+) %7B%0A            root.left.next = 
 root.
-left =
+right !
 = null 
-&&
+?
  roo
@@ -419,50 +419,32 @@
 ght 
-!= null) %7B%0A            TreeLinkNode tmp = 
+: getRootNextFirstChild(
 root
@@ -440,32 +440,33 @@
 tChild(root.next
+)
 ;%0A            th
@@ -463,42 +463,18 @@
     
+%7D%0A
     
-this.pointTo(root.right, tmp);
+    
 %0A   
@@ -482,40 +482,12 @@
     
-%7D else 
 if (
-root.left != null && 
 root
@@ -513,35 +513,36 @@
            root.
-lef
+righ
 t.next = root.ri
@@ -538,140 +538,40 @@
 t = 
-root.right;%0A            TreeLinkNode tmp = root.next;%0A            this.pointTo(root.right, tmp);%0A        %7D else %7B%0A            return
+getRootNextFirstChild(root.next)
 ;%0A  
@@ -587,37 +587,32 @@
        %0A        
-this.
 connect(root.rig
@@ -628,13 +628,8 @@
     
-this.
 conn
@@ -664,26 +664,49 @@
    p
-ublic void pointTo
+rivate TreeLinkNode getRootNextFirstChild
 (Tre
@@ -719,13 +719,23 @@
 ode 
-f
 ro
-m,
+ot) %7B%0A       
  Tre
@@ -748,13 +748,17 @@
 ode 
-to) %7B
+p = root;
 %0A   
@@ -769,18 +769,17 @@
  while (
-to
+p
  != null
@@ -798,18 +798,17 @@
     if (
-to
+p
 .left !=
@@ -832,30 +832,24 @@
         
-from.next = to
+return p
 .left;%0A 
@@ -855,34 +855,25 @@
             
-    break;
+%7D
 %0A           
@@ -865,33 +865,32 @@
   %7D%0A            
-%7D
 %0A            if 
@@ -890,18 +890,17 @@
     if (
-to
+p
 .right !
@@ -929,22 +929,16 @@
     
-from.next = to
+return p
 .rig
@@ -957,18 +957,9 @@
     
-    break;
+%7D
 %0A   
@@ -959,33 +959,32 @@
   %7D%0A            
-%7D
 %0A            to 
@@ -984,15 +984,13 @@
     
-to = to
+p = p
 .nex
@@ -998,20 +998,50 @@
 ;%0A        %7D%0A
+        %0A        return null;%0A
     %7D%0A%7D%0A

Update PopulatingNextRightPointersInEachNodeII.java