{"id":6232,"date":"2020-02-01T22:59:36","date_gmt":"2020-02-02T06:59:36","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6232"},"modified":"2020-07-25T13:24:46","modified_gmt":"2020-07-25T20:24:46","slug":"leetcode-1339-maximum-product-of-splitted-binary-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-1339-maximum-product-of-splitted-binary-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1339. Maximum Product of Splitted Binary Tree"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1339. Maximum Product of Splitted Binary Tree - \u5237\u9898\u627e\u5de5\u4f5c EP304\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/qOkuke73L_U?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>\n<\/div><\/figure>\n\n\n\n<p>Given a binary tree&nbsp;<code>root<\/code>.&nbsp;Split the binary tree into two subtrees by removing&nbsp;1 edge such that the product of the sums of the subtrees are maximized.<\/p>\n\n\n\n<p>Since the answer&nbsp;may be too large,&nbsp;return it modulo&nbsp;10^9 + 7.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/01\/21\/sample_1_1699.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [1,2,3,4,5,6]\n<strong>Output:<\/strong> 110\n<strong>Explanation:<\/strong> Remove the red edge and get 2 binary trees with sum 11 and 10. Their product is 110 (11*10)\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/01\/21\/sample_2_1699.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [1,null,2,3,4,null,null,5,6]\n<strong>Output:<\/strong> 90\n<strong>Explanation:<\/strong>  Remove the red edge and get 2 binary trees with sum 15 and 6.Their product is 90 (15*6)\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [2,3,9,10,7,8,6,5,4,11,1]\n<strong>Output:<\/strong> 1025\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [1,1]\n<strong>Output:<\/strong> 1\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Each tree has at most&nbsp;<code>50000<\/code>&nbsp;nodes and at least&nbsp;<code>2<\/code>&nbsp;nodes.<\/li><li>Each node&#8217;s value is between&nbsp;<code>[1, 10000]<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Recursion<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/1343-ep304-1.png\" alt=\"\" class=\"wp-image-6233\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/1343-ep304-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/1343-ep304-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/1343-ep304-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/1343-ep304-2.png\" alt=\"\" class=\"wp-image-6234\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/1343-ep304-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/1343-ep304-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/1343-ep304-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>Two passes:<br>First pass, compute the sum of the entire tree S.<br>Second pass, for each node, compute the sum of left\/right subtree S_l, S_r.<br>ans = max{(S &#8211; S_l) * S_l, (S &#8211; S_r) * S_r}<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maxProduct(TreeNode* root) {\n    const int kMod = 1e9 + 7;    \n    long s = 0;    \n    long ans = 0;    \n    function<int(TreeNode*)> sum = [&](TreeNode* r) {\n      if (!r) return 0;\n      int sl = sum(r->left);\n      int sr = sum(r->right);      \n      if (s) ans = max({ans, (s - sl) * sl, (s - sr) * sr});\n      return r->val + sl + sr;\n    };\n    s = sum(root);\n    sum(root);    \n    return ans % kMod;\n  }  \n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a binary tree&nbsp;root.&nbsp;Split the binary tree into two subtrees by removing&nbsp;1 edge such that the product of the sums of the subtrees are maximized.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[177,17,28],"class_list":["post-6232","post","type-post","status-publish","format-standard","hentry","category-tree","tag-medium","tag-recursion","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6232","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=6232"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6232\/revisions"}],"predecessor-version":[{"id":7142,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6232\/revisions\/7142"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}