{"id":2263,"date":"2018-03-20T21:50:15","date_gmt":"2018-03-21T04:50:15","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2263"},"modified":"2018-03-20T22:06:02","modified_gmt":"2018-03-21T05:06:02","slug":"leetcode-563-binary-tree-tilt","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-563-binary-tree-tilt\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 563. Binary Tree Tilt"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p><a href=\"https:\/\/leetcode.com\/problems\/binary-tree-tilt\/description\/\">https:\/\/leetcode.com\/problems\/binary-tree-tilt\/description\/<\/a><\/p>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u8fd4\u56de\u6811\u7684\u603btilt\u503c\u3002\u5bf9\u4e8e\u4e00\u4e2a\u8282\u70b9\u7684tilt\u503c\u5b9a\u4e49\u4e3a\u5de6\u53f3\u5b50\u6811\u5143\u7d20\u548c\u7684\u7edd\u5bf9\u4e4b\u5dee\u3002<\/p>\n<p>Given a binary tree, return the tilt of the\u00a0<b>whole tree<\/b>.<\/p>\n<p>The tilt of a\u00a0<b>tree node<\/b>\u00a0is defined as the\u00a0<b>absolute difference<\/b>\u00a0between the sum of all left subtree node values and the sum of all right subtree node values. Null node has tilt 0.<\/p>\n<p>The tilt of the\u00a0<b>whole tree<\/b>\u00a0is defined as the sum of all nodes&#8217; tilt.<\/p>\n<p><b>Example:<\/b><\/p>\n<pre class=\"crayon:false \"><b>Input:<\/b> \r\n         1\r\n       \/   \\\r\n      2     3\r\n<b>Output:<\/b> 1\r\n<b>Explanation:<\/b> \r\nTilt of node 2 : 0\r\nTilt of node 3 : 0\r\nTilt of node 1 : |2-3| = 1\r\nTilt of binary tree : 0 + 0 + 1 = 1\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>The sum of node values in any subtree won&#8217;t exceed the range of 32-bit integer.<\/li>\n<li>All the tilt values won&#8217;t exceed the range of 32-bit integer.<\/li>\n<\/ol>\n<h1>Solution: Recursion<\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(h)<\/p>\n<p>C++<\/p>\n<p>v1<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 17 ms\r\nclass Solution {\r\npublic:\r\n  int findTilt(TreeNode* root) {\r\n    int sum;\r\n    return findTilt(root, sum);\r\n  }\r\nprivate:\r\n  \/\/ return the total tilt and sum of the root.\r\n  int findTilt(TreeNode* root, int&amp; sum) {\r\n    if (root == nullptr) return 0;\r\n    int ls = 0;\r\n    int rs = 0;\r\n    int tl = findTilt(root-&gt;left, ls);\r\n    int tr = findTilt(root-&gt;right, rs);\r\n    sum = root-&gt;val + ls + rs;\r\n    return tl + tr + abs(ls - rs);    \r\n  }\r\n};<\/pre>\n<p>v1-2<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 17 ms\r\nclass Solution {\r\npublic:\r\n  int findTilt(TreeNode* root) {    \r\n    return findTiltSum(root).first;\r\n  }\r\nprivate:\r\n  pair&lt;int, int&gt; findTiltSum(TreeNode* root) {\r\n    if (root == nullptr) return {0, 0};    \r\n    auto l = findTiltSum(root-&gt;left);\r\n    auto r = findTiltSum(root-&gt;right);    \r\n    return { l.first + r.first + abs(l.second - r.second), \r\n             root-&gt;val + l.second + r.second };\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>v2<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 16 ms\r\nclass Solution {\r\npublic:\r\n  int findTilt(TreeNode* root) {\r\n    ans_ = 0;\r\n    sum(root);\r\n    return ans_;\r\n  }\r\nprivate:\r\n  int ans_;\r\n  \r\n  int sum(TreeNode* root) {\r\n    if (root == nullptr) return 0;\r\n    int l = sum(root-&gt;left);\r\n    int r = sum(root-&gt;right);\r\n    ans_ += abs(l - r);\r\n    return root-&gt;val + l + r;\r\n  }\r\n};<\/pre>\n<p>Python3<\/p>\n<pre class=\"lang:python decode:true \">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 112 ms\r\n\"\"\"\r\nclass Solution:\r\n  def findTilt(self, root):\r\n    def findTilt(root):\r\n      if not root: return 0, 0\r\n      tl, sl = findTilt(root.left)\r\n      tr, sr = findTilt(root.right)\r\n      return tl + tr + abs(sl - sr), root.val + sl + sr\r\n    return findTilt(root)[0]<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem https:\/\/leetcode.com\/problems\/binary-tree-tilt\/description\/ \u9898\u76ee\u5927\u610f\uff1a\u8fd4\u56de\u6811\u7684\u603btilt\u503c\u3002\u5bf9\u4e8e\u4e00\u4e2a\u8282\u70b9\u7684tilt\u503c\u5b9a\u4e49\u4e3a\u5de6\u53f3\u5b50\u6811\u5143\u7d20\u548c\u7684\u7edd\u5bf9\u4e4b\u5dee\u3002 Given a binary tree, return the tilt of the\u00a0whole tree. The tilt of a\u00a0tree node\u00a0is defined as the\u00a0absolute difference\u00a0between the sum of&#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":[17,62,28],"class_list":["post-2263","post","type-post","status-publish","format-standard","hentry","category-tree","tag-recursion","tag-sum","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2263","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=2263"}],"version-history":[{"count":7,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2263\/revisions"}],"predecessor-version":[{"id":2270,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2263\/revisions\/2270"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}