Python recursive solution
\u5728\u8FD9\u4E2Arecursion chapter\u5B66\u4E60\u81EA\u5DF1\u609F\u51FA\u6765\u7684\u4E00\u4E2A\u65B9\u6CD5\u3002\n\u4ECEexample\u53EF\u4EE5\u53D1\u73B0\u7ED9\u
\u5728\u8FD9\u4E2Arecursion chapter\u5B66\u4E60\u81EA\u5DF1\u609F\u51FA\u6765\u7684\u4E00\u4E2A\u65B9\u6CD5\u3002\n\u4ECEexample\u53EF\u4EE5\u53D1\u73B0\u7ED9\u7684\u89C4\u5F8B\u5C31\u662F\n\u6BCF\u4E2A\u8282\u70B9\uFF08\u975E\u53F6\u5B50\u7ED3\u70B9\uFF09\u5FC5\u6709\u4E24\u4E2A\u5B50\u8282\u70B9\n\u5BF9\u6BCF\u4E00level\u7684\u8282\u70B9\u90FD\u8FDB\u884C\u5982\u4E0B\u64CD\u4F5C\nleft.next = right\nright.next = None\n\u4F46\u662F\u8FD9\u91CC\u53EA\u662F\u8FDE\u63A5\u4E86\u4E24\u4E2A\u8282\u70B9\uFF0C\u4E00\u5C42level\u91CC\u9762\u8282\u70B9\u4E0D\u6B62\u8FD9\u4E48\u591A\uFF0C\u8FD8\u6709\u4E2D\u95F4\u7684\uFF0C\u5C31\u662F\u5DE6\u5B50\u6811\u548C\u53F3\u5B50\u6811\u7684\u4E4B\u95F4\u7684\u5DE6\u5B50\u6811\u7684\u53F3\u8282\u70B9\uFF0C\u548C\u53F3\u5B50\u6811\u7684\u5DE6\u8282\u70B9\u8981\u8FDE\u63A5\u8D77\u6765\uFF0C\u6240\u4EE5\u9012\u5F52\u5199\u4E86\u4E2D\u95F4\u7684\nconnect(left.right,right,left)\n\u867D\u7136\u6700\u540E\u5B9E\u73B0\u4E86\u7A0B\u5E8F\uFF0C\u4F46\u662F\u6539\u8FDB\u7684\u5730\u65B9\u8FD8\u662F\u6709\u5E8F\u591A\n\u5C31\u6BD4\u5982\uFF0C\u8FD9\u4E2Aright.next = None\uFF0C\u8FD9\u91CC\u64CD\u4F5C\u4E86\u591A\u6B21\uFF0C\u5982\u679C\u53EF\u4EE5\u53EA\u8981\u8FDE\u63A5\u4E00\u6B21\u5C31\u8FDE\u4E0A\u7684\u8BDD\u662F\u6700\u597D\u7684\u4E86\u3002\n```\nclass Solution(object):\n    def connect(self, root):\n        """\n        :type root: Node\n        :rtype: Node\n        """\n        if not root:\n            return None\n        root.next = None\n        def connect(left,right):\n            if left and right:\n                left.next = right\n                right.next = None\n                connect(left.left,left.right)\n                connect(left.right,right.left)\n                connect(right.left,right.right)\n        connect(root.left,root.right)\n        return root\n```\n\u5F88\u660E\u663E\u8FD9\u4E2A\u4E5F\u53EF\u4EE5\u7528BFS\u6765\u505A\uFF0C\u540C\u6837\uFF0C\u4E5F\u662F\u4E4B\u524D\u4ECE\u8BC4\u8BBA\u91CC\u9762\u5B66\u5230\u7684queue\u5B9E\u73B0\u7684BFS\u6765\u89E3\u51B3\u8FD9\u91CC\u7684\u95EE\u9898\n```\nfrom collections import deque\n\nclass Solution(object):\n    def connect(self, root):\n        """\n        :type root: Node\n        :rtype: Node\n        """\n        if not root:\n            return None\n        root.next = None\n        queue = deque([root])\n        while queue:\n            temp,size=[],len(queue)\n            for i in range(size):\n                node = queue.popleft()\n                if node:\n                    temp.append(node)\n                if node.left and node.right:\n                    queue.append(node.left)\n                    queue.append(node.right)\n            for index,node in enumerate(temp[:-1]):\n                node.next = temp[index+1]\n            temp[-1].next = None\n        return root\n```\n\u4ECE\u8BC4\u8BBA\u91CC\u9762\u5B66\u5230\u7684\u4E00\u4E2A\u65B9\u6CD5\uFF0C\u786E\u5B9E\uFF0CBFS\u53EF\u4EE5\u4F7F\u7528\uFF0C\u4F46\u662F\u6211\u4EEC\u6CA1\u6709\u5FC5\u8981\u7528\u4E00\u4E2Aqueue\u6765\u5B58\u50A8node\uFF0C\n\u56E0\u4E3Anode\u6709next\u8282\u70B9\uFF0C\u5F53\u6211\u4EEC\u6709\u4E00\u4E2A\u5934\u7ED3\u70B9\u4E4B\u540E\uFF0C\u5C31\u53EF\u4EE5\u50CF\u94FE\u8868\u4E00\u6837\u53BB\u8BBF\u95EE\u4E86\u3002\nreference\uFF1A\nhttps://leetcode.com/problems/populating-next-right-pointers-in-each-node/discuss/37465/Python-Solution-With-Explaintion\n```\nclass Solution(object):\n    def connect(self, root):\n        """\n        :type root: Node\n        :rtype: Node\n        """\n        if not root:\n            return None\n        cur = root\n        nex = cur.left\n        while cur.left:\n            cur.left.next = cur.right\n            if cur.next:\n                cur.right.next = cur.next.left\n                cur = cur.next\n            else:\n                cur = nex\n                nex = cur.left\n        return root\n```
[]