{"id":2441,"date":"2018-04-07T23:34:04","date_gmt":"2018-04-08T06:34:04","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2441"},"modified":"2018-04-07T23:42:20","modified_gmt":"2018-04-08T06:42:20","slug":"leetcode-814-binary-tree-pruning","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-814-binary-tree-pruning\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 814. Binary Tree Pruning"},"content":{"rendered":"<h1>Problem:<\/h1>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u628a\u4e0d\u542b\u67091\u7684\u8282\u70b9\u7684\u5b50\u6811\u5168\u90e8\u5220\u9664\u3002<\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/binary-tree-pruning\/description\/\">https:\/\/leetcode.com\/problems\/binary-tree-pruning\/description\/<\/a><\/p>\n<div class=\"question-description\">\n<div>\n<p>We are given the head node\u00a0<code>root<\/code>\u00a0of a binary tree, where additionally every node&#8217;s value is either a 0 or a 1.<\/p>\n<p>Return the same tree where every subtree (of the given tree) not containing a 1 has been removed.<\/p>\n<p>(Recall that the subtree of a node X is X, plus every node that is a descendant of X.)<\/p>\n<pre class=\"crayon:false\"><strong>Example 1:<\/strong>\r\n<strong>Input:<\/strong> [1,null,0,0,1]\r\n<strong>Output: <\/strong>[1,null,0,null,1]\r\n \r\n<strong>Explanation:<\/strong> \r\nOnly the red nodes satisfy the property \"every subtree not containing a 1\".\r\nThe diagram on the right represents the answer.\r\n\r\n<img decoding=\"async\" src=\"https:\/\/s3-lc-upload.s3.amazonaws.com\/uploads\/2018\/04\/06\/1028_2.png\" alt=\"\" \/>\r\n<\/pre>\n<pre class=\"crayon:false\"><strong>Example 2:<\/strong>\r\n<strong>Input:<\/strong> [1,0,1,0,0,0,1]\r\n<strong>Output: <\/strong>[1,null,1,null,1]\r\n\r\n\r\n<img decoding=\"async\" src=\"https:\/\/s3-lc-upload.s3.amazonaws.com\/uploads\/2018\/04\/06\/1028_1.png\" alt=\"\" \/>\r\n<\/pre>\n<pre class=\"crayon:false\"><strong>Example 3:<\/strong>\r\n<strong>Input:<\/strong> [1,1,0,1,1,0,1,0]\r\n<strong>Output: <\/strong>[1,1,0,1,1,null,1]\r\n\r\n\r\n<img decoding=\"async\" src=\"https:\/\/s3-lc-upload.s3.amazonaws.com\/uploads\/2018\/04\/05\/1028.png\" alt=\"\" \/>\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li>The binary tree\u00a0will\u00a0have\u00a0at\u00a0most\u00a0<code>100 nodes<\/code>.<\/li>\n<li>The value of each node will only be\u00a0<code>0<\/code>\u00a0or\u00a0<code>1<\/code>.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<h1><strong>Solution: Recursion<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(h)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 6 ms\r\nclass Solution {\r\npublic:\r\n  TreeNode* pruneTree(TreeNode* root) {\r\n    if (!root) return root;\r\n    root-&gt;left = pruneTree(root-&gt;left);\r\n    root-&gt;right = pruneTree(root-&gt;right);\r\n    if (root-&gt;val == 1 || root-&gt;left || root-&gt;right)\r\n      return root;\r\n    delete root;\r\n    return nullptr;\r\n  }\r\n};<\/pre>\n<p>Java<\/p>\n<pre class=\"lang:java decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 2 ms\r\nclass Solution {\r\n  public TreeNode pruneTree(TreeNode root) {\r\n    if (root == null) return root;\r\n    root.left = pruneTree(root.left);\r\n    root.right = pruneTree(root.right);\r\n    return (root.val == 1 || root.left != null || root.right != null) ? root : null;\r\n  }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Python3<\/p>\n<pre class=\"lang:python decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 36 ms\r\nclass Solution:\r\n  def pruneTree(self, root):\r\n    if not root: return root\r\n    root.left = self.pruneTree(root.left)\r\n    root.right = self.pruneTree(root.right)\r\n    return root if root.val == 1 or root.left or root.right else None<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: \u9898\u76ee\u5927\u610f\uff1a\u628a\u4e0d\u542b\u67091\u7684\u8282\u70b9\u7684\u5b50\u6811\u5168\u90e8\u5220\u9664\u3002 https:\/\/leetcode.com\/problems\/binary-tree-pruning\/description\/ We are given the head node\u00a0root\u00a0of a binary tree, where additionally every node&#8217;s value is either a 0 or a 1. Return&#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,292,17,28],"class_list":["post-2441","post","type-post","status-publish","format-standard","hentry","category-tree","tag-medium","tag-prunning","tag-recursion","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2441","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=2441"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2441\/revisions"}],"predecessor-version":[{"id":2445,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2441\/revisions\/2445"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}