1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/二叉树剪枝 [binary-tree-pruning].html

40 lines
1.5 KiB
HTML
Raw Normal View History

2022-03-27 20:46:41 +08:00
<p>给你二叉树的根结点&nbsp;<code>root</code>&nbsp;,此外树的每个结点的值要么是 <code>0</code> ,要么是 <code>1</code></p>
<p>返回移除了所有不包含 <code>1</code> 的子树的原二叉树。</p>
<p>节点 <code>node</code> 的子树为 <code>node</code> 本身加上所有 <code>node</code> 的后代。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/06/1028_2.png" style="width: 500px; height: 140px;" />
<pre>
<strong>输入:</strong>root = [1,null,0,0,1]
<strong>输出:</strong>[1,null,0,null,1]
<strong>解释:</strong>
只有红色节点满足条件“所有不包含 1 的子树”。 右图为返回的答案。
</pre>
<p><strong>示例 2</strong></p>
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/06/1028_1.png" style="width: 500px; height: 115px;" />
<pre>
<strong>输入:</strong>root = [1,0,1,0,0,0,1]
<strong>输出:</strong>[1,null,1,null,1]
</pre>
<p><strong>示例 3</strong></p>
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/05/1028.png" style="width: 500px; height: 134px;" />
<pre>
<strong>输入:</strong>root = [1,1,0,1,1,0,1,0]
<strong>输出:</strong>[1,1,0,1,1,null,1]
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li>树中节点的数目在范围 <code>[1, 200]</code></li>
<li><code>Node.val</code><code>0</code><code>1</code></li>
</ul>