public class PopulatingNextRightPointersinEachNode {

	public static void main(String[] args) {
		/*Test#0: Null Test*/
		TreeLinkNode nulltest = null;
		connect(nulltest);
		/*Test#1: Simple Test {1,2,3}*/
		TreeLinkNode rt = new TreeLinkNode(1);
		rt.left = new TreeLinkNode(2);
		rt.right = new TreeLinkNode(3);
		connect(rt);
		TreeLinkNode lmc = rt;
		TreeLinkNode p = rt;
		while(lmc != null){
			System.out.print(p.val + ", ");
			p = p.next;
			if(p == null){
				System.out.print("#, ");
				lmc = lmc.left;
				p = lmc;
			}
			
		}

	}

    public static void connect(TreeLinkNode root) {
        TreeLinkNode parent = root;
        if(parent != null){
        
	    	TreeLinkNode child = parent.left;
	    	TreeLinkNode leftmostchild = child;
	    	
	    	while(leftmostchild != null){
	    		if(child == parent.left){
	    			child.next = parent.right;
	    			//System.out.println("Hello");
	    		}
	    		else{
	    			parent = parent.next;
	    			if(parent != null){
	    				child.next = parent.left;
	    			}
	    		}
	    		child = child.next;
	    		
	    		if(child == null){
	    			parent = leftmostchild;
	    			child = parent.left;
	    			leftmostchild = child;
	    		}
	    	}
        }
    }
}

@@ -1,12 +1,999 @@
+/**%0A * @author = Chi Chen%0A * @email = chenchiapply@gmail.com%0A * @version = 1.0%0A * @runtime = 392ms%0A */%0A  %0A/**%0A *@description%0A *%0A * Given a binary tree%0A *  %0A * struct TreeLinkNode %7B%0A * %09TreeLinkNode *left;%0A * %09TreeLinkNode *right;%0A * %09TreeLinkNode *next;%0A * %7D%0A * %0A * Populate each next pointer to point to its next right node. %0A * If there is no next right node, the next pointer should be set to NULL.%0A * Initially, all next pointers are set to NULL.%0A *  %0A * Note:%0A * %09You may only use constant extra space.%0A * %09You may assume that it is a perfect binary tree %0A * %09(ie, all leaves are at the same level, and every parent has two children).%0A * %09%0A * For example,%0A * %09Given the following perfect binary tree,%0A * %0A * %09%09  1%0A * %09%09/  %5C%0A *             2    3%0A *           / %5C  / %5C%0A *          4  5  6  7%0A *  %0A * After calling your function, the tree should look like:%0A *    %0A *              1 -%3E NULL%0A *            /  %5C%0A *          2 -%3E 3 -%3E NULL%0A *        / %5C  / %5C%0A *       4-%3E5-%3E6-%3E7 -%3E NULL%0A%0A
 public class

Update PopulatingNextRightPointersinEachNode.java