@@ -0,0 +1,1034 @@
+/*%0A%0A */%0A%0Aclass TreeNodeWithRightPointer %7B%0A%09int val;%0A%09TreeNodeWithRightPointer left;%0A%09TreeNodeWithRightPointer right;%0A%09TreeNodeWithRightPointer rightPointer;%0A%7D%0Apublic class ConnectRightPointer %7B%0A%09public void connectRightPointer(TreeNodeWithRightPointer root) %7B%0A%09%09if(root==null) %7B%0A%09%09%09return;%0A%09%09%7D%0A%09%09TreeNodeWithRightPointer cur=root;%0A%09%09while (cur!=null) %7B%0A%09%09%09TreeNodeWithRightPointer next_level=cur;%0A%09%09%09while (cur!=null) %7B%0A%09%09%09%09if(cur.left!=null) %7B%0A%09%09%09%09%09if(cur.right!=null) %7B%0A%09%09%09%09%09%09cur.left.rightPointer=cur.right;%0A%09%09%09%09%09%7Delse %7B%0A%09%09%09%09%09%09cur.left.rightPointer=cur.rightPointer;%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%09if(cur.right!=null) %7B%0A%09%09%09%09%09cur.right.rightPointer=cur.rightPointer;%0A%09%09%09%09%7D%0A%09%09%09%09cur=cur.rightPointer;%0A%09%09%09%7D%0A%09%09%09cur=findNext(next_level);%0A%09%09%7D%0A%0A%09%7D%0A%0A%09private TreeNodeWithRightPointer findNext(TreeNodeWithRightPointer next_level) %7B%0A%09%09while (next_level!=null) %7B%0A%09%09%09if(next_level.left!=null) %7B%0A%09%09%09%09return next_level.left;%0A%09%09%09%7D%0A%09%09%09if(next_level.right!=null) %7B%0A%09%09%09%09return next_level.right;%0A%09%09%09%7D%0A%09%09%09next_level=next_level.rightPointer;%0A%09%09%7D%0A%09%09return null;%0A%09%7D%0A%7D%0A

set right pointer for tree