package com.fishercoder.solutions;

import com.fishercoder.common.classes.TreeLinkNode;

/**
 * 116. Populating Next Right Pointers in Each Node

 Given a binary tree

 struct TreeLinkNode {
     TreeLinkNode *left;
     TreeLinkNode *right;
     TreeLinkNode *next;
 }
 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.

 Initially, all next pointers are set to NULL.

 Note:

 You may only use constant extra space.
 You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
 For example,
 Given the following perfect binary tree,
      1
    /  \
   2    3
  / \  / \
 4  5  6  7

 After calling your function, the tree should look like:
      1 -> NULL
    /  \
   2 -> 3 -> NULL
  / \  / \
 4->5->6->7 -> NULL
 */

public class _116 {
  public static class Solution1 {
    /**
     * credit: https://discuss.leetcode.com/topic/1106/o-1-space-o-n-complexity-iterative-solution
     * 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 curr = root;  //current node of current level

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

@@ -86,786 +86,8 @@
 e;%0A%0A
-/**%0A * 116. Populating Next Right Pointers in Each Node%0A%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%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
 publ
@@ -102,16 +102,18 @@
  _116 %7B%0A
+  
   public
@@ -138,16 +138,20 @@
 tion1 %7B%0A
+    
     /**%0A
@@ -155,16 +155,20 @@
 **%0A     
+  
+  
 * credit
@@ -258,16 +258,20 @@
 on%0A     
+    
 * based 
@@ -304,11 +304,19 @@
     
+   
+ 
 */%0A
+    
     
@@ -359,24 +359,30 @@
 t) %7B%0A%0A      
+   
+   
 TreeLinkNode
@@ -412,32 +412,38 @@
  the next level%0A
+      
       TreeLinkNo
@@ -491,24 +491,30 @@
  next level%0A
+      
       TreeLi
@@ -569,24 +569,30 @@
 evel%0A%0A      
+  
+    
 while (curr 
@@ -594,32 +594,40 @@
 curr != null) %7B%0A
+        
         while (c
@@ -682,16 +682,26 @@
         
+     
+     
 //left c
@@ -697,32 +697,42 @@
    //left child%0A
+          
           if (cu
@@ -754,32 +754,44 @@
 ) %7B%0A            
+   
+         
 if (prev != null
@@ -800,32 +800,46 @@
 %7B%0A              
+        
+      
 prev.next = curr
@@ -837,32 +837,44 @@
 xt = curr.left;%0A
+            
             %7D el
@@ -870,32 +870,46 @@
        %7D else %7B%0A
+              
               he
@@ -928,34 +928,58 @@
 ft;%0A            
+   
-%7D%0A
+         %7D%0A            
             prev
@@ -994,34 +994,48 @@
 left;%0A          
-%7D%0A
+          %7D%0A    
           //righ
@@ -1028,16 +1028,22 @@
         
+      
 //right 
@@ -1040,32 +1040,42 @@
   //right child%0A
+          
           if (cu
@@ -1098,32 +1098,44 @@
 ) %7B%0A            
+  
+          
 if (prev != null
@@ -1144,32 +1144,46 @@
 %7B%0A              
+       
+       
 prev.next = curr
@@ -1194,32 +1194,44 @@
 ht;%0A            
+           
+ 
 %7D else %7B%0A       
@@ -1215,32 +1215,46 @@
        %7D else %7B%0A
+              
               he
@@ -1274,34 +1274,58 @@
 ht;%0A            
+  
-%7D%0A
+          %7D%0A            
             prev
@@ -1341,34 +1341,47 @@
 ight;%0A          
-%7D%0A
+          %7D%0A   
           //move
@@ -1366,32 +1366,39 @@
  %7D%0A             
+       
 //move to next n
@@ -1407,24 +1407,34 @@
 e%0A          
+  
+        
 curr = curr.
@@ -1439,34 +1439,50 @@
 r.next;%0A        
+ 
-%7D%0A
+       %7D%0A        
         //move t
@@ -1498,24 +1498,32 @@
 vel%0A        
+  
+      
 curr = head;
@@ -1527,24 +1527,32 @@
 ad;%0A        
+   
+     
 head = null;
@@ -1556,24 +1556,32 @@
 ll;%0A        
+   
+     
 prev = null;
@@ -1591,16 +1591,28 @@
     
+    
-%7D%0A  
   %7D%0A
+        %7D%0A  
   %7D%0A

refactor 116