[Python3] BFS and DFS
BFS\n\n"""\n# Definition for a Node.\nclass Node:\n    def __init__(self, val, left, right, next):\n        self.val = val\n        self.left = left\n        se
* BFS\n```\n"""\n# Definition for a Node.\nclass Node:\n    def __init__(self, val, left, right, next):\n        self.val = val\n        self.left = left\n        self.right = right\n        self.next = next\n"""\nfrom collections import deque\nclass Solution:\n    def connect(self, root: \'Node\') -> \'Node\':\n        """\n      1 (1)\n    2 (2)->    3(1)\n 4(4) -> 5(3) -> 6(2) -> 7(1)\n\n"""\n        if root ==  None:\n            return None\n        q = deque([root])\n        while q:  # [1] [3,4,5]\n            size = len(q) # 1 2\n            while size > 0: #  > 0\n                node = q.popleft() # node =1,2,3\n                if size > 1 :# \n                    node.next = q[0] #  2.next = 3\n                size -= 1          # size =1\n                \n                if node.left:         \n                    q.append(node.left)\n                if node.right:\n                    q.append(node.right)\n        return root\n```\n* DFS\n```\n"""\n# Definition for a Node.\nclass Node:\n    def __init__(self, val, left, right, next):\n        self.val = val\n        self.left = left\n        self.right = right\n        self.next = next\n"""\nfrom collections import deque\nclass Solution:\n    def connect(self, root: \'Node\') -> \'Node\':\n        """\n      1 (1)\n    2 (2)->    3(1)\n 4(4) -> 5(3) -> 6(2) -> 7(1)\n\n"""\n        self.dfs(root)\n        return root\n    \n    ## (1). left child -> right child\n    ## (2). right child -> next.left child\n    def dfs(self,root):\n        if root == None or root.left == None:\n            return\n        root.left.next = root.right\n        if root.next != None: \n            root.right.next = root.next.left\n        self.dfs(root.left)\n        self.dfs(root.right)\n```
['Breadth-First Search', 'Python', 'Python3']