{"id":957,"date":"2025-12-14T03:12:20","date_gmt":"2025-12-13T19:12:20","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=957"},"modified":"2025-12-14T03:12:20","modified_gmt":"2025-12-13T19:12:20","slug":"leetcode-124-%e4%ba%8c%e5%8f%89%e6%a0%91%e4%b8%ad%e7%9a%84%e6%9c%80%e5%a4%a7%e8%b7%af%e5%be%84%e5%92%8c","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=957","title":{"rendered":"LeetCode 124 &#8211; \u4e8c\u53c9\u6811\u4e2d\u7684\u6700\u5927\u8def\u5f84\u548c"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u4e8c\u53c9\u6811\u4e2d\u7684 \u8def\u5f84 \u88ab\u5b9a\u4e49\u4e3a\u4e00\u6761\u8282\u70b9\u5e8f\u5217\uff0c\u5e8f\u5217\u4e2d\u6bcf\u5bf9\u76f8\u90bb\u8282\u70b9\u4e4b\u95f4\u90fd\u5b58\u5728\u4e00\u6761\u8fb9\u3002\u540c\u4e00\u4e2a\u8282\u70b9\u5728\u4e00\u6761\u8def\u5f84\u5e8f\u5217\u4e2d \u81f3\u591a\u51fa\u73b0\u4e00\u6b21 \u3002\u8be5\u8def\u5f84 \u81f3\u5c11\u5305\u542b\u4e00\u4e2a \u8282\u70b9\uff0c\u4e14\u4e0d\u4e00\u5b9a\u7ecf\u8fc7\u6839\u8282\u70b9\u3002<br \/>\n\u8def\u5f84\u548c \u662f\u8def\u5f84\u4e2d\u5404\u8282\u70b9\u503c\u7684\u603b\u548c\u3002<br \/>\n\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9 root \uff0c\u8fd4\u56de\u5176 \u6700\u5927\u8def\u5f84\u548c \u3002<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<p>\u6811\u4e2d\u4efb\u610f\u4e00\u6761\u8def\u5f84\uff0c\u79bb root \u6700\u8fd1\u7684\u7ed3\u70b9\u53ef\u4ee5\u89c6\u4e3a\u8def\u5f84\u7684\u6839\u7ed3\u70b9\u3002<br \/>\n\u5bf9\u8def\u5f84\u7684\u6839\u7ed3\u70b9\u6765\u8bf4\uff0c\u6700\u5927\u8def\u5f84 = \u6839\u7ed3\u70b9\u503c + max(\u5de6\u7ed3\u70b9\u51fa\u53d1\u6700\u957f\u8def\u5f84\uff0c\u53f3\u7ed3\u70b9\u51fa\u53d1\u6700\u957f\u8def\u5f84\uff0c\u5de6\u7ed3\u70b9\u51fa\u53d1\u6700\u957f\u8def\u5f84 + \u53f3\u7ed3\u70b9\u51fa\u53d1\u6700\u957f\u8def\u5f84)\u3002<br \/>\n\u56e0\u6b64\u53ea\u9700\u8981\u9012\u5f52\u8ba1\u7b97\u4ece\u5f53\u524d\u7ed3\u70b9\u51fa\u53d1\u7684\u6700\u957f\u8def\u5f84\uff0c\u5728\u8fc7\u7a0b\u4e2d\u679a\u4e3e\u8def\u5f84\u7684\u6839\u7ed3\u70b9\uff0c\u7ef4\u62a4\u6700\u5927\u8def\u5f84\u503c\u3002<br \/>\n\u6bcf\u4e2a\u7ed3\u70b9\u53ea\u8bbf\u95ee\u4e00\u6b21\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">private int ans = -1001;\n\npublic int maxPathSum(TreeNode root) {\n    dfs(root);\n    return ans;\n}\n\nprivate int dfs(TreeNode root) {\n    if (root == null) {\n        return -1001;\n    }\n    int left = dfs(root.left);\n    int right = dfs(root.right);\n    int max = Math.max(left, right);\n\n    ans = Math.max(ans, root.val);\n    ans = Math.max(ans, max + root.val);\n    ans = Math.max(ans, left + right + root.val);\n\n    int length = Math.max(max, 0);\n    return length + root.val;\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">private var ans = -1001\n\nfun maxPathSum(root: TreeNode?): Int {\n    dfs(root)\n    return ans\n}\n\n\/\/ \u4ece root \u51fa\u53d1\u6700\u957f\u8def\u5f84\nprivate fun dfs(root: TreeNode?): Int {\n    if (root == null) {\n        return -1001\n    }\n    val left = dfs(root.left)\n    val right = dfs(root.right)\n    val max = max(left, right)\n\n    ans = ans.coerceAtLeast(root.`val`)\n        .coerceAtLeast(max + root.`val`)\n        .coerceAtLeast(left + right + root.`val`)\n\n    return max.coerceAtLeast(0) + root.`val`\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u4e8c\u53c9\u6811\u4e2d\u7684 \u8def\u5f84 \u88ab\u5b9a\u4e49\u4e3a\u4e00\u6761\u8282\u70b9\u5e8f\u5217\uff0c\u5e8f\u5217\u4e2d\u6bcf\u5bf9\u76f8\u90bb\u8282\u70b9\u4e4b\u95f4\u90fd\u5b58\u5728\u4e00\u6761\u8fb9\u3002\u540c\u4e00\u4e2a\u8282\u70b9\u5728\u4e00\u6761\u8def\u5f84\u5e8f\u5217 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[11,20,33],"class_list":["post-957","post","type-post","status-publish","format-standard","hentry","category-algo","tag-dfs","tag-leetcode","tag-33"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/957","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=957"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/957\/revisions"}],"predecessor-version":[{"id":958,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/957\/revisions\/958"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=957"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=957"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=957"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}