{"id":701,"date":"2020-09-26T11:12:22","date_gmt":"2020-09-26T03:12:22","guid":{"rendered":"http:\/\/39.96.58.60\/?p=701"},"modified":"2022-10-18T16:38:16","modified_gmt":"2022-10-18T08:38:16","slug":"leetcode-129-%e6%b1%82%e6%a0%b9%e5%88%b0%e5%8f%b6%e5%ad%90%e8%8a%82%e7%82%b9%e6%95%b0%e5%ad%97%e4%b9%8b%e5%92%8c","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=701","title":{"rendered":"Leetcode 129. \u6c42\u6839\u5230\u53f6\u5b50\u8282\u70b9\u6570\u5b57\u4e4b\u548c"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811\uff0c\u5b83\u7684\u6bcf\u4e2a\u7ed3\u70b9\u90fd\u5b58\u653e\u4e00\u4e2a&nbsp;<code>0-9<\/code>&nbsp;\u7684\u6570\u5b57\uff0c\u6bcf\u6761\u4ece\u6839\u5230\u53f6\u5b50\u8282\u70b9\u7684\u8def\u5f84\u90fd\u4ee3\u8868\u4e00\u4e2a\u6570\u5b57\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u4ece\u6839\u5230\u53f6\u5b50\u8282\u70b9\u8def\u5f84 <code>1-&gt;2-&gt;3<\/code> \u4ee3\u8868\u6570\u5b57 <code>123<\/code>\u3002<\/p>\n<p>\u8ba1\u7b97\u4ece\u6839\u5230\u53f6\u5b50\u8282\u70b9\u751f\u6210\u7684\u6240\u6709\u6570\u5b57\u4e4b\u548c\u3002<\/p>\n<p><strong>\u8bf4\u660e:<\/strong>&nbsp;\u53f6\u5b50\u8282\u70b9\u662f\u6307\u6ca1\u6709\u5b50\u8282\u70b9\u7684\u8282\u70b9\u3002<\/p>\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> [1,2,3]\n\n    1\n\n   \/ \\\n\n  2   3\n\n<strong>\u8f93\u51fa:<\/strong> 25\n\n<strong>\u89e3\u91ca:<\/strong>\n\n\u4ece\u6839\u5230\u53f6\u5b50\u8282\u70b9\u8def\u5f84 <code>1-&gt;2<\/code> \u4ee3\u8868\u6570\u5b57 <code>12<\/code>.\n\n\u4ece\u6839\u5230\u53f6\u5b50\u8282\u70b9\u8def\u5f84 <code>1-&gt;3<\/code> \u4ee3\u8868\u6570\u5b57 <code>13<\/code>.\n\n\u56e0\u6b64\uff0c\u6570\u5b57\u603b\u548c = 12 + 13 = <code>25<\/code>.<\/pre>\n<p><strong>\u793a\u4f8b 2:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> [4,9,0,5,1]\n\n    4\n\n   \/ \\\n\n  9   0\n\n&nbsp;\/ \\\n\n5   1\n\n<strong>\u8f93\u51fa:<\/strong> 1026\n\n<strong>\u89e3\u91ca:<\/strong>\n\n\u4ece\u6839\u5230\u53f6\u5b50\u8282\u70b9\u8def\u5f84 <code>4-&gt;9-&gt;5<\/code> \u4ee3\u8868\u6570\u5b57 495.\n\n\u4ece\u6839\u5230\u53f6\u5b50\u8282\u70b9\u8def\u5f84 <code>4-&gt;9-&gt;1<\/code> \u4ee3\u8868\u6570\u5b57 491.\n\n\u4ece\u6839\u5230\u53f6\u5b50\u8282\u70b9\u8def\u5f84 <code>4-&gt;0<\/code> \u4ee3\u8868\u6570\u5b57 40.\n\n\u56e0\u6b64\uff0c\u6570\u5b57\u603b\u548c = 495 + 491 + 40 = <code>1026<\/code>.<\/pre>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u6811\u3001 \u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a40 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8681.35% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.8 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e865.27% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u9012\u5f52\uff0c\u83b7\u5f97\u6bcf\u6761\u652f\u7ebf\u8282\u70b9\u7ec4\u6210\u7684\u6570\u5b57\uff0c\u7136\u540e\u76f8\u52a0\n\"\"\"\nclass Solution:\n    def sumNumbers(self, root: TreeNode) -&gt; int:\n        record = []\n\n        def find(root, current):\n            current += str(root.val)\n            if root.left == None and root.right == None:\n                record.append(int(current))\n                return\n            else:\n                if root.left:\n                    find(root.left, current)\n                if root.right:\n                    find(root.right, current)\n\n        if root:\n            find(root, '')\n\n        return sum(record)<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811\uff0c\u5b83\u7684\u6bcf\u4e2a\u7ed3\u70b9\u90fd\u5b58\u653e\u4e00\u4e2a&nbsp;0-9&nbsp;\u7684\u6570\u5b57\uff0c\u6bcf\u6761\u4ece\u6839\u5230\u53f6\u5b50\u8282\u70b9\u7684\u8def\u5f84\u90fd\u4ee3\u8868\u4e00\u4e2a&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/701"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=701"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/701\/revisions"}],"predecessor-version":[{"id":702,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/701\/revisions\/702"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=701"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}