✅ [Python] Two Solutions || BFS and DFS || Image Explanation || Beginner Friendly
PLEASE UPVOTE if you like \uD83D\uDE01 If you have any question, feel free to ask. \n Solution 1\n\t Standard BFS with mantaining pre_level and pre_node as the 
**PLEASE UPVOTE if you like** \uD83D\uDE01 **If you have any question, feel free to ask.** \n* **Solution 1**\n\t* Standard BFS with mantaining `pre_level` and `pre_node` as the previous node in BFS sequence\n\t\t* `level == pre_level` means current `node` is not the first node of `level`, then `pre_node.next = node` and update `pre_node = node`\n\t\t* `else` means `pre_level < level` and `node` is the first node of `level`, then no need to update `pre_node.next`, leave it as `None`, update `pre_node = node` only.\n\t\t* standard BFS, append `node.left` and `node.right` to the queue\n\t```\n\tTime  Complexity: O(N)\n\tSpace Complexity: O(N)\n\t```\n\t```\n\tclass Solution(object):\n    def connect(self, root):\n        if root is None: return None\n        dq, pre_level, pre_node = deque([(1, root)]), 0, None\n        while dq:\n            level, node = dq.popleft()\n            if level == pre_level:  # current node is not the first node of level\n                pre_node.next = node\n                pre_node = node\n            else:  # pre_level < level and node is the first node of level, then no need to update pre_node.next, \n                # leave it as None, update pre_node = node only.\n                pre_level, pre_node = level, node\n            if node.left:  # root is a perfect binary tree, once left exists, right must also exist\n                dq.append((level + 1, node.left))\n                dq.append((level + 1, node.right))\n        return root\n\t```\n\n* **Solution 2**\n\t* Recursive DFS, the current root node is responsible for linking the nodes on both sides closest to the central axis for all levels.\n\t![image](https://assets.leetcode.com/users/images/4e546a8b-d11e-49a9-859d-6c7e5092e8cb_1640762771.0732064.jpeg)\n\n\t\n\t```\n\tTime  Complexity: O(N)\n\tSpace Complexity: O(1)\n\t```\n\t```\n\tclass Solution(object):\n    def connect(self, root):\n        if not root: return root\n        if root.left: \n            left, right = root.left, root.right\n            self.connect(left)\n            self.connect(right)\n            while left:\n                left.next = right\n                left, right = left.right, right.left\n        return root\n\t```\n\t\n**PLEASE UPVOTE if you like \uD83D\uDE01 If you have any question, feel free to ask.**
['Depth-First Search', 'Breadth-First Search', 'Python']