<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>prunning Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/prunning/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/prunning/</link>
	<description></description>
	<lastBuildDate>Sun, 08 Apr 2018 06:42:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>prunning Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/prunning/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 814. Binary Tree Pruning</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-814-binary-tree-pruning/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-814-binary-tree-pruning/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 08 Apr 2018 06:34:04 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[prunning]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2441</guid>

					<description><![CDATA[<p>Problem: 题目大意：把不含有1的节点的子树全部删除。 https://leetcode.com/problems/binary-tree-pruning/description/ We are given the head node root of a binary tree, where additionally every node&#8217;s value is either a 0 or a 1. Return&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-814-binary-tree-pruning/">花花酱 LeetCode 814. Binary Tree Pruning</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1>Problem:</h1>
<p>题目大意：把不含有1的节点的子树全部删除。</p>
<p><a href="https://leetcode.com/problems/binary-tree-pruning/description/">https://leetcode.com/problems/binary-tree-pruning/description/</a></p>
<div class="question-description">
<div>
<p>We are given the head node <code>root</code> of a binary tree, where additionally every node&#8217;s value is either a 0 or a 1.</p>
<p>Return the same tree where every subtree (of the given tree) not containing a 1 has been removed.</p>
<p>(Recall that the subtree of a node X is X, plus every node that is a descendant of X.)</p>
<pre class="crayon:false"><strong>Example 1:</strong>
<strong>Input:</strong> [1,null,0,0,1]
<strong>Output: </strong>[1,null,0,null,1]
 
<strong>Explanation:</strong> 
Only the red nodes satisfy the property "every subtree not containing a 1".
The diagram on the right represents the answer.

<img src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/06/1028_2.png" alt="" />
</pre>
<pre class="crayon:false"><strong>Example 2:</strong>
<strong>Input:</strong> [1,0,1,0,0,0,1]
<strong>Output: </strong>[1,null,1,null,1]


<img src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/06/1028_1.png" alt="" />
</pre>
<pre class="crayon:false"><strong>Example 3:</strong>
<strong>Input:</strong> [1,1,0,1,1,0,1,0]
<strong>Output: </strong>[1,1,0,1,1,null,1]


<img src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/05/1028.png" alt="" />
</pre>
<p><strong>Note:</strong></p>
<ul>
<li>The binary tree will have at most <code>100 nodes</code>.</li>
<li>The value of each node will only be <code>0</code> or <code>1</code>.</li>
</ul>
</div>
</div>
<h1><strong>Solution: Recursion</strong></h1>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(h)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 6 ms
class Solution {
public:
  TreeNode* pruneTree(TreeNode* root) {
    if (!root) return root;
    root-&gt;left = pruneTree(root-&gt;left);
    root-&gt;right = pruneTree(root-&gt;right);
    if (root-&gt;val == 1 || root-&gt;left || root-&gt;right)
      return root;
    delete root;
    return nullptr;
  }
};</pre><p>Java</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 2 ms
class Solution {
  public TreeNode pruneTree(TreeNode root) {
    if (root == null) return root;
    root.left = pruneTree(root.left);
    root.right = pruneTree(root.right);
    return (root.val == 1 || root.left != null || root.right != null) ? root : null;
  }
}</pre><p>&nbsp;</p>
<p>Python3</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 36 ms
class Solution:
  def pruneTree(self, root):
    if not root: return root
    root.left = self.pruneTree(root.left)
    root.right = self.pruneTree(root.right)
    return root if root.val == 1 or root.left or root.right else None</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-814-binary-tree-pruning/">花花酱 LeetCode 814. Binary Tree Pruning</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/tree/leetcode-814-binary-tree-pruning/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
