{"id":1148830,"date":"2025-01-13T16:42:00","date_gmt":"2025-01-13T08:42:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1148830.html"},"modified":"2025-01-13T16:42:03","modified_gmt":"2025-01-13T08:42:03","slug":"python%e5%a6%82%e4%bd%95%e7%9b%b4%e6%8e%a5%e8%b7%b3%e5%87%ba%e9%80%92%e5%bd%92","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1148830.html","title":{"rendered":"python\u5982\u4f55\u76f4\u63a5\u8df3\u51fa\u9012\u5f52"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25172550\/43f1e7c1-8139-4b49-bacc-bb221bfa771b.webp\" alt=\"python\u5982\u4f55\u76f4\u63a5\u8df3\u51fa\u9012\u5f52\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u53ef\u4ee5\u901a\u8fc7<code>return<\/code>\u8bed\u53e5\u3001<code>try...except<\/code>\u7ed3\u6784\u3001\u8bbe\u7f6e\u6807\u5fd7\u53d8\u91cf\u7b49\u65b9\u5f0f\u76f4\u63a5\u8df3\u51fa\u9012\u5f52\u3002<\/strong>\u5176\u4e2d\u6700\u5e38\u89c1\u4e14\u76f4\u89c2\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>return<\/code>\u8bed\u53e5\uff0c\u5f53\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u65f6\uff0c\u76f4\u63a5\u8fd4\u56de\u7ed3\u679c\u4ee5\u8df3\u51fa\u9012\u5f52\u3002<strong>\u4f7f\u7528<code>return<\/code>\u8bed\u53e5\u4e0d\u4ec5\u53ef\u4ee5\u7ec8\u6b62\u5f53\u524d\u9012\u5f52\u8c03\u7528\uff0c\u8fd8\u53ef\u4ee5\u5c06\u7ed3\u679c\u4f20\u9012\u56de\u4e0a\u4e00\u7ea7\u8c03\u7528\uff0c\u4ece\u800c\u9010\u6b65\u7ed3\u675f\u6574\u4e2a\u9012\u5f52\u8fc7\u7a0b\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u4e00\u70b9\uff0c\u8003\u8651\u4ee5\u4e0b\u4f8b\u5b50\uff1a\u6211\u4eec\u5728\u5bfb\u627e\u4e00\u4e2a\u5d4c\u5957\u5217\u8868\u4e2d\u7684\u6700\u5927\u503c\u3002\u5229\u7528\u9012\u5f52\u7b97\u6cd5\uff0c\u6bcf\u6b21\u67e5\u627e\u5b50\u5217\u8868\u4e2d\u7684\u6700\u5927\u503c\uff0c\u5e76\u5728\u67d0\u4e2a\u6761\u4ef6\u6ee1\u8db3\u65f6\u4f7f\u7528<code>return<\/code>\u8bed\u53e5\u76f4\u63a5\u8fd4\u56de\u6700\u5927\u503c\uff0c\u4ece\u800c\u8df3\u51fa\u9012\u5f52\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def find_max(lst):<\/p>\n<p>    if not lst:<\/p>\n<p>        return float(&#39;-inf&#39;)<\/p>\n<p>    elif isinstance(lst[0], list):<\/p>\n<p>        return max(find_max(lst[0]), find_max(lst[1:]))<\/p>\n<p>    else:<\/p>\n<p>        return max(lst[0], find_max(lst[1:]))<\/p>\n<p>nested_list = [1, [4, [6, 7]], [8, [10, 12]], 3]<\/p>\n<p>print(find_max(nested_list))  # \u8f93\u51fa: 12<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\uff0c\u5f53\u5217\u8868\u4e3a\u7a7a\u65f6\uff0c\u9012\u5f52\u4f1a\u76f4\u63a5\u8fd4\u56de\u8d1f\u65e0\u7a77\u5927\uff08<code>float(&#39;-inf&#39;)<\/code>\uff09\uff0c\u8fd9\u8868\u793a\u6211\u4eec\u5df2\u7ecf\u904d\u5386\u5b8c\u5f53\u524d\u5b50\u5217\u8868\uff0c\u5e76\u4e14\u4e0d\u4f1a\u518d\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u9012\u5f52\u8c03\u7528\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001<code>return<\/code>\u8bed\u53e5\u8df3\u51fa\u9012\u5f52<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>return<\/code>\u8bed\u53e5\u662fPython\u4e2d\u7528\u6765\u9000\u51fa\u51fd\u6570\u5e76\u8fd4\u56de\u7ed3\u679c\u7684\u5173\u952e\u5b57\u3002\u5728\u9012\u5f52\u51fd\u6570\u4e2d\uff0c<code>return<\/code>\u8bed\u53e5\u4e0d\u4ec5\u80fd\u9000\u51fa\u5f53\u524d\u7684\u9012\u5f52\u8c03\u7528\uff0c\u8fd8\u80fd\u5c06\u7ed3\u679c\u4f20\u9012\u56de\u4e0a\u4e00\u7ea7\u8c03\u7528\uff0c\u9010\u6b65\u7ed3\u675f\u6574\u4e2a\u9012\u5f52\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>2. \u5e94\u7528\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><p>\u8003\u8651\u4e00\u4e2a\u6c42\u9636\u4e58\u7684\u9012\u5f52\u51fd\u6570\uff0c\u5f53\u9012\u5f52\u5230<code>n = 1<\/code>\u65f6\u76f4\u63a5\u8fd4\u56de<code>1<\/code>\uff0c\u4ece\u800c\u8df3\u51fa\u9012\u5f52\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def factorial(n):<\/p>\n<p>    if n == 1:<\/p>\n<p>        return 1<\/p>\n<p>    else:<\/p>\n<p>        return n * factorial(n - 1)<\/p>\n<p>print(factorial(5))  # \u8f93\u51fa: 120<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5f53<code>n<\/code>\u7b49\u4e8e<code>1<\/code>\u65f6\uff0c<code>return 1<\/code>\u8bed\u53e5\u4f1a\u76f4\u63a5\u8fd4\u56de<code>1<\/code>\uff0c\u5e76\u7ed3\u675f\u5f53\u524d\u7684\u9012\u5f52\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001<code>try...except<\/code>\u7ed3\u6784\u8df3\u51fa\u9012\u5f52<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u590d\u6742\u7684\u9012\u5f52\u7b97\u6cd5\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u901a\u8fc7\u5f02\u5e38\u5904\u7406\u7ed3\u6784\u6765\u8df3\u51fa\u9012\u5f52\u3002<code>try...except<\/code>\u7ed3\u6784\u5141\u8bb8\u6211\u4eec\u5728\u9047\u5230\u5f02\u5e38\u65f6\u4e2d\u65ad\u9012\u5f52\uff0c\u5e76\u6267\u884c\u76f8\u5e94\u7684\u5904\u7406\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><h3>2. \u5e94\u7528\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><p>\u8003\u8651\u4e00\u4e2a\u5728\u5d4c\u5957\u5217\u8868\u4e2d\u67e5\u627e\u7279\u5b9a\u5143\u7d20\u7684\u9012\u5f52\u51fd\u6570\uff0c\u5f53\u627e\u5230\u8be5\u5143\u7d20\u65f6\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\uff0c\u4ece\u800c\u8df3\u51fa\u9012\u5f52\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Found(Exception):<\/p>\n<p>    pass<\/p>\n<p>def find_element(lst, target):<\/p>\n<p>    try:<\/p>\n<p>        for element in lst:<\/p>\n<p>            if isinstance(element, list):<\/p>\n<p>                find_element(element, target)<\/p>\n<p>            elif element == target:<\/p>\n<p>                r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se Found<\/p>\n<p>    except Found:<\/p>\n<p>        print(f&quot;Element {target} found!&quot;)<\/p>\n<p>nested_list = [1, [4, [6, 7]], [8, [10, 12]], 3]<\/p>\n<p>find_element(nested_list, 10)  # \u8f93\u51fa: Element 10 found!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5f53\u627e\u5230\u76ee\u6807\u5143\u7d20\u65f6\uff0c<code>raise Found<\/code>\u8bed\u53e5\u4f1a\u629b\u51fa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5f02\u5e38\uff0c\u4ece\u800c\u7ec8\u6b62\u5f53\u524d\u7684\u9012\u5f52\u8c03\u7528\uff0c\u5e76\u8df3\u51fa\u6574\u4e2a\u9012\u5f52\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u8bbe\u7f6e\u6807\u5fd7\u53d8\u91cf\u8df3\u51fa\u9012\u5f52<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e\u5168\u5c40\u6216\u975e\u5c40\u90e8\u6807\u5fd7\u53d8\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u63a7\u5236\u9012\u5f52\u51fd\u6570\u7684\u6267\u884c\u6d41\u7a0b\u3002\u5f53\u6807\u5fd7\u53d8\u91cf\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u65f6\uff0c\u76f4\u63a5\u8fd4\u56de\u4ee5\u8df3\u51fa\u9012\u5f52\u3002<\/p>\n<\/p>\n<p><h3>2. \u5e94\u7528\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><p>\u8003\u8651\u4e00\u4e2a\u5728\u5d4c\u5957\u5217\u8868\u4e2d\u67e5\u627e\u7279\u5b9a\u5143\u7d20\u7684\u9012\u5f52\u51fd\u6570\uff0c\u901a\u8fc7\u8bbe\u7f6e\u6807\u5fd7\u53d8\u91cf\u6765\u8df3\u51fa\u9012\u5f52\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">found = False<\/p>\n<p>def find_element(lst, target):<\/p>\n<p>    global found<\/p>\n<p>    if found:<\/p>\n<p>        return<\/p>\n<p>    for element in lst:<\/p>\n<p>        if isinstance(element, list):<\/p>\n<p>            find_element(element, target)<\/p>\n<p>        elif element == target:<\/p>\n<p>            found = True<\/p>\n<p>            print(f&quot;Element {target} found!&quot;)<\/p>\n<p>            return<\/p>\n<p>nested_list = [1, [4, [6, 7]], [8, [10, 12]], 3]<\/p>\n<p>find_element(nested_list, 10)  # \u8f93\u51fa: Element 10 found!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u8bbe\u7f6e\u5168\u5c40\u53d8\u91cf<code>found<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u63a7\u5236\u9012\u5f52\u51fd\u6570\u7684\u6267\u884c\u6d41\u7a0b\u3002\u5f53\u627e\u5230\u76ee\u6807\u5143\u7d20\u65f6\uff0c\u8bbe\u7f6e<code>found<\/code>\u4e3a<code>True<\/code>\u5e76\u76f4\u63a5\u8fd4\u56de\uff0c\u4ece\u800c\u8df3\u51fa\u9012\u5f52\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u9012\u5f52\u4f18\u5316\u4e0e\u6027\u80fd\u8003\u8651<\/h2>\n<\/p>\n<p><h3>1. \u9012\u5f52\u6df1\u5ea6\u9650\u5236<\/h3>\n<\/p>\n<p><p>Python\u9ed8\u8ba4\u7684\u9012\u5f52\u6df1\u5ea6\u9650\u5236\u4e3a1000\u5c42\uff0c\u5982\u679c\u9012\u5f52\u5c42\u6b21\u8fc7\u6df1\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4<code>RecursionError<\/code>\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<code>sys.setrecursionlimit<\/code>\u51fd\u6570\u6765\u8c03\u6574\u9012\u5f52\u6df1\u5ea6\u9650\u5236\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fc7\u6df1\u7684\u9012\u5f52\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6808\u6ea2\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>sys.setrecursionlimit(2000)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5c3e\u9012\u5f52\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5c3e\u9012\u5f52\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u9012\u5f52\u5f62\u5f0f\uff0c\u5728\u51fd\u6570\u7684\u6700\u540e\u4e00\u6b65\u8c03\u7528\u81ea\u8eab\u3002\u67d0\u4e9b\u7f16\u7a0b\u8bed\u8a00\u652f\u6301\u5c3e\u9012\u5f52\u4f18\u5316\uff0c\u53ef\u4ee5\u907f\u514d\u9012\u5f52\u8c03\u7528\u5806\u6808\u7684\u589e\u957f\u3002\u7136\u800c\uff0cPython\u5e76\u4e0d\u652f\u6301\u5c3e\u9012\u5f52\u4f18\u5316\uff0c\u56e0\u6b64\u5728\u5904\u7406\u6df1\u5ea6\u9012\u5f52\u65f6\uff0c\u4ecd\u9700\u8c28\u614e\u3002<\/p>\n<\/p>\n<p><h3>3. \u52a8\u6001\u89c4\u5212\u4e0e\u9012\u5f52<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u95ee\u9898\u4e2d\uff0c\u901a\u8fc7\u5c06\u9012\u5f52\u4e0e\u52a8\u6001\u89c4\u5212\u7ed3\u5408\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7b97\u6cd5\u7684\u6548\u7387\u3002\u4f8b\u5982\uff0c\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u8ba1\u7b97\u53ef\u4ee5\u901a\u8fc7\u8bb0\u5fc6\u5316\u9012\u5f52\uff08Memoization\uff09\u6765\u4f18\u5316\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def fibonacci(n, memo={}):<\/p>\n<p>    if n in memo:<\/p>\n<p>        return memo[n]<\/p>\n<p>    if n &lt;= 2:<\/p>\n<p>        return 1<\/p>\n<p>    memo[n] = fibonacci(n - 1, memo) + fibonacci(n - 2, memo)<\/p>\n<p>    return memo[n]<\/p>\n<p>print(fibonacci(50))  # \u8f93\u51fa: 12586269025<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u4f7f\u7528\u5b57\u5178<code>memo<\/code>\u6765\u5b58\u50a8\u5df2\u8ba1\u7b97\u7684\u7ed3\u679c\uff0c\u907f\u514d\u4e86\u91cd\u590d\u8ba1\u7b97\uff0c\u63d0\u9ad8\u4e86\u9012\u5f52\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u9012\u5f52\u4e0e\u8fed\u4ee3\u7684\u8f6c\u6362<\/h2>\n<\/p>\n<p><h3>1. \u9012\u5f52\u8f6c\u8fed\u4ee3<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u9012\u5f52\u7b97\u6cd5\u8f6c\u6362\u4e3a\u8fed\u4ee3\u7b97\u6cd5\uff0c\u4ee5\u907f\u514d\u9012\u5f52\u8c03\u7528\u5e26\u6765\u7684\u6027\u80fd\u95ee\u9898\u3002\u901a\u8fc7\u4f7f\u7528\u6808\u6216\u961f\u5217\uff0c\u6211\u4eec\u53ef\u4ee5\u6a21\u62df\u9012\u5f52\u7684\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>2. \u5e94\u7528\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><p>\u8003\u8651\u4e00\u4e2a\u9012\u5f52\u5b9e\u73b0\u7684\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff08DFS\uff09\u7b97\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a\u8fed\u4ee3\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def dfs_recursive(graph, start, visited=None):<\/p>\n<p>    if visited is None:<\/p>\n<p>        visited = set()<\/p>\n<p>    visited.add(start)<\/p>\n<p>    for neighbor in graph[start]:<\/p>\n<p>        if neighbor not in visited:<\/p>\n<p>            dfs_recursive(graph, neighbor, visited)<\/p>\n<p>    return visited<\/p>\n<p>def dfs_iterative(graph, start):<\/p>\n<p>    visited = set()<\/p>\n<p>    stack = [start]<\/p>\n<p>    while stack:<\/p>\n<p>        vertex = stack.pop()<\/p>\n<p>        if vertex not in visited:<\/p>\n<p>            visited.add(vertex)<\/p>\n<p>            stack.extend(set(graph[vertex]) - visited)<\/p>\n<p>    return visited<\/p>\n<p>graph = {<\/p>\n<p>    &#39;A&#39;: [&#39;B&#39;, &#39;C&#39;],<\/p>\n<p>    &#39;B&#39;: [&#39;D&#39;, &#39;E&#39;],<\/p>\n<p>    &#39;C&#39;: [&#39;F&#39;],<\/p>\n<p>    &#39;D&#39;: [],<\/p>\n<p>    &#39;E&#39;: [&#39;F&#39;],<\/p>\n<p>    &#39;F&#39;: []<\/p>\n<p>}<\/p>\n<p>print(dfs_recursive(graph, &#39;A&#39;))  # \u8f93\u51fa: {&#39;A&#39;, &#39;C&#39;, &#39;B&#39;, &#39;E&#39;, &#39;D&#39;, &#39;F&#39;}<\/p>\n<p>print(dfs_iterative(graph, &#39;A&#39;))  # \u8f93\u51fa: {&#39;A&#39;, &#39;B&#39;, &#39;E&#39;, &#39;D&#39;, &#39;C&#39;, &#39;F&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u9012\u5f52\u5b9e\u73b0\u7684DFS\u7b97\u6cd5\u901a\u8fc7\u9012\u5f52\u8c03\u7528\u6765\u904d\u5386\u56fe\uff0c\u800c\u8fed\u4ee3\u5b9e\u73b0\u7684DFS\u7b97\u6cd5\u4f7f\u7528\u6808\u6765\u6a21\u62df\u9012\u5f52\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u9012\u5f52\u5e94\u7528\u5b9e\u4f8b<\/h2>\n<\/p>\n<p><h3>1. \u4e8c\u53c9\u6811\u904d\u5386<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u662f\u5904\u7406\u6811\u7ed3\u6784\u7684\u5e38\u89c1\u65b9\u6cd5\u4e4b\u4e00\u3002\u4f8b\u5982\uff0c\u4e8c\u53c9\u6811\u7684\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u548c\u540e\u5e8f\u904d\u5386\u90fd\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class TreeNode:<\/p>\n<p>    def __init__(self, val=0, left=None, right=None):<\/p>\n<p>        self.val = val<\/p>\n<p>        self.left = left<\/p>\n<p>        self.right = right<\/p>\n<p>def preorder_traversal(root):<\/p>\n<p>    if root is None:<\/p>\n<p>        return []<\/p>\n<p>    return [root.val] + preorder_traversal(root.left) + preorder_traversal(root.right)<\/p>\n<p>def inorder_traversal(root):<\/p>\n<p>    if root is None:<\/p>\n<p>        return []<\/p>\n<p>    return inorder_traversal(root.left) + [root.val] + inorder_traversal(root.right)<\/p>\n<p>def postorder_traversal(root):<\/p>\n<p>    if root is None:<\/p>\n<p>        return []<\/p>\n<p>    return postorder_traversal(root.left) + postorder_traversal(root.right) + [root.val]<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u4e8c\u53c9\u6811<\/strong><\/h2>\n<p>root = TreeNode(1)<\/p>\n<p>root.left = TreeNode(2)<\/p>\n<p>root.right = TreeNode(3)<\/p>\n<p>root.left.left = TreeNode(4)<\/p>\n<p>root.left.right = TreeNode(5)<\/p>\n<p>print(preorder_traversal(root))  # \u8f93\u51fa: [1, 2, 4, 5, 3]<\/p>\n<p>print(inorder_traversal(root))   # \u8f93\u51fa: [4, 2, 5, 1, 3]<\/p>\n<p>print(postorder_traversal(root)) # \u8f93\u51fa: [4, 5, 2, 3, 1]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u516b\u7687\u540e\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u516b\u7687\u540e\u95ee\u9898\u662f\u7ecf\u5178\u7684\u9012\u5f52\u56de\u6eaf\u7b97\u6cd5\u5e94\u7528\u5b9e\u4f8b\u3002\u76ee\u6807\u662f\u57288&#215;8\u7684\u68cb\u76d8\u4e0a\u6446\u653e8\u4e2a\u7687\u540e\uff0c\u4f7f\u5f97\u5b83\u4eec\u5f7c\u6b64\u4e0d\u53d7\u653b\u51fb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def solve_n_queens(n):<\/p>\n<p>    def is_valid(board, row, col):<\/p>\n<p>        for i in range(row):<\/p>\n<p>            if board[i] == col or abs(board[i] - col) == abs(i - row):<\/p>\n<p>                return False<\/p>\n<p>        return True<\/p>\n<p>    def place_queens(board, row):<\/p>\n<p>        if row == n:<\/p>\n<p>            result.append(board[:])<\/p>\n<p>            return<\/p>\n<p>        for col in range(n):<\/p>\n<p>            if is_valid(board, row, col):<\/p>\n<p>                board[row] = col<\/p>\n<p>                place_queens(board, row + 1)<\/p>\n<p>                board[row] = -1<\/p>\n<p>    result = []<\/p>\n<p>    board = [-1] * n<\/p>\n<p>    place_queens(board, 0)<\/p>\n<p>    return result<\/p>\n<p>solutions = solve_n_queens(8)<\/p>\n<p>print(f&quot;Total solutions: {len(solutions)}&quot;)<\/p>\n<p>for solution in solutions:<\/p>\n<p>    print(solution)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u9012\u5f52\u56de\u6eaf\u7b97\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u627e\u5230\u6240\u6709\u53ef\u80fd\u7684\u516b\u7687\u540e\u6446\u653e\u65b9\u6848\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u9012\u5f52\u51fd\u6570\u8c03\u8bd5\u6280\u5de7<\/h2>\n<\/p>\n<p><h3>1. \u6dfb\u52a0\u6253\u5370\u8c03\u8bd5\u4fe1\u606f<\/h3>\n<\/p>\n<p><p>\u5728\u9012\u5f52\u51fd\u6570\u4e2d\u6dfb\u52a0\u6253\u5370\u8c03\u8bd5\u4fe1\u606f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4e86\u89e3\u9012\u5f52\u8c03\u7528\u7684\u8fc7\u7a0b\u3002\u4f8b\u5982\uff0c\u8c03\u8bd5\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u9012\u5f52\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def fibonacci(n):<\/p>\n<p>    print(f&quot;Calling fibonacci({n})&quot;)<\/p>\n<p>    if n &lt;= 2:<\/p>\n<p>        return 1<\/p>\n<p>    return fibonacci(n - 1) + fibonacci(n - 2)<\/p>\n<p>print(fibonacci(5))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u6253\u5370\u8c03\u8bd5\u4fe1\u606f\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u6bcf\u6b21\u9012\u5f52\u8c03\u7528\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\u3002<\/p>\n<\/p>\n<p><h3>2. \u4f7f\u7528\u8c03\u8bd5\u5668<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u8c03\u8bd5\u5668\uff08\u5982Python\u5185\u7f6e\u7684<code>pdb<\/code>\u6a21\u5757\uff09\uff0c\u53ef\u4ee5\u9010\u6b65\u6267\u884c\u9012\u5f52\u51fd\u6570\uff0c\u67e5\u770b\u53d8\u91cf\u7684\u503c\u548c\u9012\u5f52\u8c03\u7528\u6808\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pdb<\/p>\n<p>def fibonacci(n):<\/p>\n<p>    pdb.set_trace()<\/p>\n<p>    if n &lt;= 2:<\/p>\n<p>        return 1<\/p>\n<p>    return fibonacci(n - 1) + fibonacci(n - 2)<\/p>\n<p>print(fibonacci(5))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8c03\u8bd5\u5668\uff0c\u6211\u4eec\u53ef\u4ee5\u9010\u6b65\u6267\u884c\u4ee3\u7801\uff0c\u67e5\u770b\u6bcf\u6b21\u9012\u5f52\u8c03\u7528\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h2>\u516b\u3001\u9012\u5f52\u51fd\u6570\u7684\u5b9e\u9645\u5e94\u7528<\/h2>\n<\/p>\n<p><h3>1. \u6587\u4ef6\u5939\u904d\u5386<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u51fd\u6570\u5e38\u7528\u4e8e\u904d\u5386\u6587\u4ef6\u5939\u7ed3\u6784\u3002\u4f8b\u5982\uff0c\u9012\u5f52\u904d\u5386\u4e00\u4e2a\u6587\u4ef6\u5939\u53ca\u5176\u5b50\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def list_files(directory):<\/p>\n<p>    for entry in os.listdir(directory):<\/p>\n<p>        path = os.path.join(directory, entry)<\/p>\n<p>        if os.path.isdir(path):<\/p>\n<p>            list_files(path)<\/p>\n<p>        else:<\/p>\n<p>            print(path)<\/p>\n<p>list_files(&quot;\/path\/to\/directory&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u9012\u5f52\u8c03\u7528<code>list_files<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u904d\u5386\u6307\u5b9a\u76ee\u5f55\u53ca\u5176\u5b50\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>2. \u9012\u5f52\u6c42\u89e3\u6570\u72ec<\/h3>\n<\/p>\n<p><p>\u6570\u72ec\u662f\u4e00\u79cd\u7ecf\u5178\u7684\u903b\u8f91\u8c1c\u9898\uff0c\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u56de\u6eaf\u7b97\u6cd5\u6c42\u89e3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_valid(board, row, col, num):<\/p>\n<p>    for i in range(9):<\/p>\n<p>        if board[row][i] == num or board[i][col] == num:<\/p>\n<p>            return False<\/p>\n<p>    start_row, start_col = 3 * (row \/\/ 3), 3 * (col \/\/ 3)<\/p>\n<p>    for i in range(3):<\/p>\n<p>        for j in range(3):<\/p>\n<p>            if board[start_row + i][start_col + j] == num:<\/p>\n<p>                return False<\/p>\n<p>    return True<\/p>\n<p>def solve_sudoku(board):<\/p>\n<p>    for row in range(9):<\/p>\n<p>        for col in range(9):<\/p>\n<p>            if board[row][col] == 0:<\/p>\n<p>                for num in range(1, 10):<\/p>\n<p>                    if is_valid(board, row, col, num):<\/p>\n<p>                        board[row][col] = num<\/p>\n<p>                        if solve_sudoku(board):<\/p>\n<p>                            return True<\/p>\n<p>                        board[row][col] = 0<\/p>\n<p>                return False<\/p>\n<p>    return True<\/p>\n<p>sudoku_board = [<\/p>\n<p>    [5, 3, 0, 0, 7, 0, 0, 0, 0],<\/p>\n<p>    [6, 0, 0, 1, 9, 5, 0, 0, 0],<\/p>\n<p>    [0, 9, 8, 0, 0, 0, 0, 6, 0],<\/p>\n<p>    [8, 0, 0, 0, 6, 0, 0, 0, 3],<\/p>\n<p>    [4, 0, 0, 8, 0, 3, 0, 0, 1],<\/p>\n<p>    [7, 0, 0, 0, 2, 0, 0, 0, 6],<\/p>\n<p>    [0, 6, 0, 0, 0, 0, 2, 8, 0],<\/p>\n<p>    [0, 0, 0, 4, 1, 9, 0, 0, 5],<\/p>\n<p>    [0, 0, 0, 0, 8, 0, 0, 7, 9]<\/p>\n<p>]<\/p>\n<p>solve_sudoku(sudoku_board)<\/p>\n<p>for row in sudoku_board:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u9012\u5f52\u56de\u6eaf\u7b97\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u627e\u5230\u6570\u72ec\u7684\u89e3\uff0c\u5e76\u6253\u5370\u51fa\u5b8c\u6574\u7684\u6570\u72ec\u68cb\u76d8\u3002<\/p>\n<\/p>\n<p><h2>\u4e5d\u3001\u9012\u5f52\u51fd\u6570\u7684\u4f18\u7f3a\u70b9<\/h2>\n<\/p>\n<p><h3>1. \u4f18\u70b9<\/h3>\n<\/p>\n<ul>\n<li><strong>\u7b80\u6d01\u6613\u61c2<\/strong>\uff1a\u9012\u5f52\u51fd\u6570\u901a\u5e38\u8f83\u4e3a\u7b80\u6d01\uff0c\u4ee3\u7801\u6613\u4e8e\u7406\u89e3\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u5206\u800c\u6cbb\u4e4b\u7684\u7b97\u6cd5\u3002<\/li>\n<li><strong>\u89e3\u51b3\u590d\u6742\u95ee\u9898<\/strong>\uff1a\u9012\u5f52\u51fd\u6570\u53ef\u4ee5\u65b9\u4fbf\u5730\u89e3\u51b3\u4e00\u4e9b\u590d\u6742\u7684\u3001\u5177\u6709\u9012\u5f52\u6027\u8d28\u7684\u95ee\u9898\uff0c\u5982\u6811\u7684\u904d\u5386\u3001\u5168\u6392\u5217\u3001\u56fe\u7684\u904d\u5386\u7b49\u3002<\/li>\n<\/ul>\n<p><h3>2. \u7f3a\u70b9<\/h3>\n<\/p>\n<ul>\n<li><strong>\u6027\u80fd\u95ee\u9898<\/strong>\uff1a\u9012\u5f52\u51fd\u6570\u53ef\u80fd\u5bfc\u81f4\u8f83\u9ad8\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u5c24\u5176\u662f\u6df1\u5ea6\u9012\u5f52\u65f6\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6808\u6ea2\u51fa\u3002<\/li>\n<li><strong>\u8c03\u8bd5\u56f0\u96be<\/strong>\uff1a\u9012\u5f52\u51fd\u6570\u7684\u8c03\u8bd5\u76f8\u5bf9\u590d\u6742\uff0c\u5c24\u5176\u662f\u5728\u9012\u5f52\u5c42\u6b21\u8f83\u6df1\u65f6\uff0c\u7406\u89e3\u9012\u5f52\u8c03\u7528\u6808\u4e2d\u7684\u72b6\u6001\u8f83\u4e3a\u56f0\u96be\u3002<\/li>\n<\/ul>\n<p><h2>\u5341\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u76f4\u63a5\u8df3\u51fa\u9012\u5f52\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u4f7f\u7528<code>return<\/code>\u8bed\u53e5\u3001<code>try...except<\/code>\u7ed3\u6784\u548c\u8bbe\u7f6e\u6807\u5fd7\u53d8\u91cf\u3002<strong><code>return<\/code>\u8bed\u53e5\u662f\u6700\u5e38\u89c1\u4e14\u76f4\u89c2\u7684\u65b9\u6cd5<\/strong>\uff0c\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u7ec8\u6b62\u5f53\u524d\u9012\u5f52\u8c03\u7528\uff0c\u8fd8\u53ef\u4ee5\u5c06\u7ed3\u679c\u4f20\u9012\u56de\u4e0a\u4e00\u7ea7\u8c03\u7528\uff0c\u4ece\u800c\u9010\u6b65\u7ed3\u675f\u6574\u4e2a\u9012\u5f52\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9012\u5f52\u51fd\u6570\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u573a\u666f\uff0c\u5982\u6811\u7684\u904d\u5386\u3001\u56fe\u7684\u904d\u5386\u3001\u6587\u4ef6\u5939\u904d\u5386\u3001\u6570\u72ec\u6c42\u89e3\u7b49\u3002\u7136\u800c\uff0c\u9012\u5f52\u51fd\u6570\u4e5f\u5b58\u5728\u4e00\u4e9b\u6027\u80fd\u95ee\u9898\u548c\u8c03\u8bd5\u56f0\u96be\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528\u9012\u5f52\u7b97\u6cd5\u65f6\uff0c\u9700\u8981\u6743\u8861\u5176\u4f18\u7f3a\u70b9\uff0c\u5e76\u7ed3\u5408\u5177\u4f53\u95ee\u9898\u9009\u62e9\u5408\u9002\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002\u901a\u8fc7\u4f18\u5316\u9012\u5f52\u7b97\u6cd5\u3001\u7ed3\u5408\u52a8\u6001\u89c4\u5212\u7b49\u6280\u672f\uff0c\u53ef\u4ee5\u63d0\u9ad8\u9012\u5f52\u7b97\u6cd5\u7684\u6548\u7387\uff0c\u89e3\u51b3\u590d\u6742\u7684\u5b9e\u9645\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5728\u9012\u5f52\u51fd\u6570\u4e2d\u5904\u7406\u7279\u5b9a\u6761\u4ef6\u4ee5\u7ec8\u6b62\u9012\u5f52\uff1f<\/strong><br \/>\u5728\u9012\u5f52\u51fd\u6570\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u7279\u5b9a\u6761\u4ef6\u6765\u51b3\u5b9a\u4f55\u65f6\u7ec8\u6b62\u9012\u5f52\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>if<\/code>\u8bed\u53e5\u6765\u68c0\u67e5\u67d0\u4e2a\u6761\u4ef6\u662f\u5426\u6ee1\u8db3\uff0c\u5982\u679c\u6ee1\u8db3\uff0c\u5c31\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u76f4\u63a5\u8df3\u51fa\u9012\u5f52\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u80fd\u6709\u6548\u505c\u6b62\u9012\u5f52\uff0c\u8fd8\u80fd\u907f\u514d\u6f5c\u5728\u7684\u65e0\u9650\u5faa\u73af\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u5728\u9012\u5f52\u4e2d\u9700\u8981\u8fd4\u56de\u591a\u4e2a\u503c\uff0c\u5e94\u8be5\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5728\u9012\u5f52\u51fd\u6570\u4e2d\u8fd4\u56de\u591a\u4e2a\u503c\u53ef\u4ee5\u4f7f\u7528\u5143\u7ec4\u6216\u5217\u8868\u3002\u901a\u8fc7\u5c06\u591a\u4e2a\u503c\u6253\u5305\u5728\u4e00\u8d77\u8fd4\u56de\uff0c\u8c03\u7528\u9012\u5f52\u51fd\u6570\u65f6\u80fd\u591f\u83b7\u53d6\u5230\u6240\u6709\u9700\u8981\u7684\u4fe1\u606f\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u8c03\u7528\u9012\u5f52\u51fd\u6570\u7684\u5730\u65b9\uff0c\u786e\u4fdd\u6b63\u786e\u89e3\u5305\u8fd4\u56de\u7684\u5143\u7ec4\u6216\u5217\u8868\uff0c\u4ee5\u4fbf\u80fd\u591f\u4f7f\u7528\u6bcf\u4e2a\u8fd4\u56de\u503c\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f18\u5316\u9012\u5f52\u51fd\u6570\u4ee5\u63d0\u9ad8\u6027\u80fd\uff1f<\/strong><br \/>\u4f18\u5316\u9012\u5f52\u51fd\u6570\u7684\u5e38\u89c1\u65b9\u6cd5\u662f\u4f7f\u7528\u7f13\u5b58\u6280\u672f\uff0c\u5373\u8bb0\u5fc6\u5316\uff08memoization\uff09\u3002\u901a\u8fc7\u5b58\u50a8\u5df2\u7ecf\u8ba1\u7b97\u8fc7\u7684\u7ed3\u679c\uff0c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u9012\u5f52\u51fd\u6570\u7684\u6027\u80fd\u3002Python\u4e2d\u7684<code>functools.lru_cache<\/code>\u88c5\u9970\u5668\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u4f7f\u5f97\u9012\u5f52\u8c03\u7528\u53d8\u5f97\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u53ef\u4ee5\u901a\u8fc7return\u8bed\u53e5\u3001try&#8230;except\u7ed3\u6784\u3001\u8bbe\u7f6e\u6807\u5fd7\u53d8\u91cf\u7b49\u65b9\u5f0f\u76f4\u63a5\u8df3\u51fa\u9012\u5f52\u3002\u5176\u4e2d\u6700 [&hellip;]","protected":false},"author":3,"featured_media":1148839,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1148830"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1148830"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1148830\/revisions"}],"predecessor-version":[{"id":1148841,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1148830\/revisions\/1148841"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1148839"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1148830"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1148830"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1148830"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}