{"id":622,"date":"2017-10-17T19:10:04","date_gmt":"2017-10-18T02:10:04","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=622"},"modified":"2018-09-04T22:34:06","modified_gmt":"2018-09-05T05:34:06","slug":"leetcode-124-binary-tree-maximum-path-sum","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-124-binary-tree-maximum-path-sum\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 124. Binary Tree Maximum Path Sum"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 124. Binary Tree Maximum Path Sum  - \u5237\u9898\u627e\u5de5\u4f5c EP90\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/9ZNky1wqNUw?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>Given a binary tree, find the maximum path sum.<\/p>\n<p>For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain\u00a0<b>at least one node<\/b>\u00a0and does not need to go through the root.<\/p>\n<p>For example:<br \/>\nGiven the below binary tree,<\/p>\n<pre>       1\r\n      \/ \\\r\n     2   3\r\n<\/pre>\n<p>Return\u00a0<code>6<\/code>.<\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-2404451723245401\" data-ad-slot=\"7983117522\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>Recursion<\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/124-ep90-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-626\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/124-ep90-1.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/124-ep90-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/124-ep90-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/124-ep90-1-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/124-ep90-1-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p>Time complexity O(n)<\/p>\n<p>Space complexity O(h)<\/p>\n<h1><strong>Solution: Recursion<\/strong><\/h1>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Runtime: 19 ms\r\nclass Solution {\r\npublic:\r\n    int maxPathSum(TreeNode* root) {\r\n        if (!root) return 0;\r\n        int ans = INT_MIN;\r\n        maxPathSum(root, ans);\r\n        return ans;\r\n    }\r\nprivate:\r\n    int maxPathSum(TreeNode* root, int&amp; ans) {\r\n        if (!root) return 0;\r\n        int l = max(0, maxPathSum(root-&gt;left, ans));\r\n        int r = max(0, maxPathSum(root-&gt;right, ans));\r\n        int sum = l + r + root-&gt;val;\r\n        ans = max(ans, sum);\r\n        return max(l, r) + root-&gt;val;\r\n    }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true \">\"\"\"\r\nAuthor: Huahua\r\nRuntime: 138 ms (&lt; 90.38%)\r\n\"\"\"\r\nclass Solution(object):\r\n    def _maxPathSum(self, root):\r\n        if not root: return -sys.maxint\r\n        l = max(0, self._maxPathSum(root.left))\r\n        r = max(0, self._maxPathSum(root.right))\r\n        self.ans = max(self.ans, root.val + l + r)\r\n        return root.val + max(l, r)\r\n        \r\n    def maxPathSum(self, root):\r\n        self.ans = -sys.maxint\r\n        self._maxPathSum(root)\r\n        return self.ans<\/pre>\n<\/div><\/div>\n<p><strong>Related Problems:<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-687-longest-univalue-path\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 687. Longest Univalue Path<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-543-diameter-of-binary-tree\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 543. Diameter of Binary Tree<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[165,45],"tags":[217,132,28],"class_list":["post-622","post","type-post","status-publish","format-standard","hentry","category-hard","category-tree","tag-hard","tag-path-sum","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/622","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=622"}],"version-history":[{"count":7,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/622\/revisions"}],"predecessor-version":[{"id":3871,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/622\/revisions\/3871"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=622"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}