@@ -0,0 +1,1853 @@
+/***%0A * Given a binary tree%0A *%0A *     struct TreeLinkNode %7B%0A *           TreeLinkNode *left;%0A *           TreeLinkNode *right;%0A *           TreeLinkNode *next;%0A *     %7D%0A *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.%0A *%0A *Initially, all next pointers are set to NULL.%0A *%0A *Note:%0A *%0A *You may only use constant extra space.%0A *You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).%0A *For example,%0A *Given the following perfect binary tree,%0A *         1%0A *        /  %5C%0A *       2    3%0A *      / %5C  / %5C%0A *     4  5  6  7%0A * After calling your function, the tree should look like:%0A *          1 -%3E NULL%0A *         /  %5C%0A *        2 -%3E 3 -%3E NULL%0A *       / %5C  / %5C%0A *      4-%3E5-%3E6-%3E7 -%3E NULL%0A */%0A%0A// Definition for binary tree with next pointer.%0Aprivate class TreeLinkNode %7B%0A    int val;%0A    TreeLinkNode left, right, next;%0A    TreeLinkNode(int x) %7B val = x; %7D%0A%7D%0A%0Apublic class Solution %7B%0A    // Solution 1: O(n) O(log n)%0A    public void connect(TreeLinkNode root) %7B%0A        connect(root, null);%0A    %7D%0A    private void connect(TreeLinkNode root, TreeLinkNode next) %7B%0A        if (root == null)%0A            return;%0A        else %0A            root.next = next;%0A%0A        connect(root.left, root.right);%0A        if (next != null)%0A            connect(root.right, next.left); // Bug here...%0A        else%0A            connect(root.right, null);%0A    %7D%0A%0A    // Solution 2: O(n), O(log n)%0A    public void connect(TreeLinkNode root) %7B%0A        if (root == null)%0A            return;%0A        if (root.left != null)%0A            root.left.next = root.right;%0A        if (root.right != null)%0A            root.right.next = root.next == null? null : root.next.left;%0A        connect(root.left);%0A        connect(root.right);%0A    %7D%0A%7D%0A

add PopulatingNextRightPointersinEachNode.java