https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/



Runtime: 66 ms, faster than 68.36% of Python3 online submissions for Populating Next Right Pointers in Each Node II.

Memory Usage: 15.3 MB, less than 48.33% of Python3 online submissions for Populating Next Right Pointers in Each Node II.



```python
"""
# Definition for a Node.
class Node:
    def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
        self.val = val
        self.left = left
        self.right = right
        self.next = next
"""

class Solution:
    def connect(self, root: 'Node') -> 'Node':
        # travel is copied from my old code
        # 8:45
        level_list = self.travel(root)
        for row in level_list:
            #print([node.val for node in row])
            for index, node in enumerate(row):
                if index >= len(row) - 1:
                    node.next = None
                else:
                    node.next = row[index+1]
        return root
        # 8:53
        # debug until 9:00
        
    def travel(self, root):
        if (root is None):
            return []
        
        list_of_vals = []
        level_list = [[root]]
        
        while True:
            current_level = level_list[-1]
            #print(current_level)
            level_list.append([])
            i = 0
            while i < len(current_level):
                node = current_level[i]

                list_of_vals.append(node.val)

                if node.left:
                    level_list[-1].append(node.left)

                if node.right:
                    level_list[-1].append(node.right)
                
                i += 1
            
            if len(level_list[-1]) == 0:
                break

            list_of_vals.append(f"__{len(level_list)}__")
                
        return level_list
```


___



"Fear is the number 1 reason that stops people from following their dreams" --- LiYang
