package com.fishercoder.solutions;

import com.fishercoder.common.classes.TreeLinkNode;

/**
 * 117. Populating Next Right Pointers in Each Node II
 *
 * Follow up for problem "Populating Next Right Pointers in Each Node".
 * What if the given tree could be any binary tree? Would your previous solution still work?

 Note:

 You may only use constant extra space.
 For example,
 Given the following binary tree,
      1
    /  \
   2    3
  / \    \
 4   5    7
 After calling your function, the tree should look like:
      1 -> NULL
    /  \
   2 -> 3 -> NULL
  / \    \
 4-> 5 -> 7 -> NULL */

public class _117 {
    public static class Solution1 {
        /**credit: https://discuss.leetcode.com/topic/1106/o-1-space-o-n-complexity-iterative-solution
        O(1) space, based on level order traversal*/
        public void connect(TreeLinkNode root) {

            TreeLinkNode head = null; //head of the next level
            TreeLinkNode prev = null; //the leading node on the next level
            TreeLinkNode cur = root;  //current node of current level

            while (cur != null) {

                while (cur != null) { //iterate on the current level
                    //left child
                    if (cur.left != null) {
                        if (prev != null) {
                            prev.next = cur.left;
                        } else {
                            head = cur.left;
                        }
                        prev = cur.left;
                    }
                    //right child
                    if (cur.right != null) {
                        if (prev != null) {
                            prev.next = cur.right;
                        } else {
                            head = cur.right;
                        }
                        prev = cur.right;
                    }
                    //move to next node
                    cur = cur.next;
                }

                //move to next level
                cur = head;
                head = null;
                prev = null;
            }
        }
    }
}

@@ -86,517 +86,8 @@
 e;%0A%0A
-/**%0A * 117. Populating Next Right Pointers in Each Node II%0A *%0A * Follow up for problem %22Populating Next Right Pointers in Each Node%22.%0A * What if the given tree could be any binary tree? Would your previous solution still work?%0A%0A Note:%0A%0A You may only use constant extra space.%0A For example,%0A Given the following binary tree,%0A      1%0A    /  %5C%0A   2    3%0A  / %5C    %5C%0A 4   5    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-%3E 5 -%3E 7 -%3E NULL */%0A%0A
 publ
@@ -149,16 +149,28 @@
      /**
+%0A         * 
 credit: 
@@ -260,16 +260,19 @@
 %0A       
+  *
  O(1) sp
@@ -306,16 +306,26 @@
 raversal
+%0A         
 */%0A     

refactor 117