mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
51 lines
1.7 KiB
HTML
51 lines
1.7 KiB
HTML
<p>给定一个二叉树 <strong>根节点</strong> <code>root</code> ,树的每个节点的值要么是 <code>0</code>,要么是 <code>1</code>。请剪除该二叉树中所有节点的值为 <code>0</code> 的子树。</p>
|
|
|
|
<p>节点 <code>node</code> 的子树为 <code>node</code> 本身,以及所有 <code>node</code> 的后代。</p>
|
|
|
|
<p> </p>
|
|
|
|
<p><strong>示例 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>输入:</strong> [1,null,0,0,1]
|
|
<strong>输出: </strong>[1,null,0,null,1]
|
|
<strong>解释:</strong>
|
|
只有红色节点满足条件“所有不包含 1 的子树”。
|
|
右图为返回的答案。
|
|
|
|
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/06/1028_2.png" style="width:450px" />
|
|
</pre>
|
|
|
|
<p><strong>示例 2:</strong></p>
|
|
|
|
<pre>
|
|
<strong>输入:</strong> [1,0,1,0,0,0,1]
|
|
<strong>输出: </strong>[1,null,1,null,1]
|
|
<strong>解释:</strong>
|
|
|
|
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/06/1028_1.png" style="width:450px" />
|
|
</pre>
|
|
|
|
<p><strong>示例 3:</strong></p>
|
|
|
|
<pre>
|
|
<strong>输入:</strong> [1,1,0,1,1,0,1,0]
|
|
<strong>输出: </strong>[1,1,0,1,1,null,1]
|
|
<strong>解释:</strong>
|
|
|
|
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/05/1028.png" style="width:450px" />
|
|
</pre>
|
|
|
|
<p> </p>
|
|
|
|
<p><strong>提示:</strong></p>
|
|
|
|
<ul>
|
|
<li>二叉树的节点个数的范围是 <code>[1,200]</code></li>
|
|
<li>二叉树节点的值只会是 <code>0</code> 或 <code>1</code></li>
|
|
</ul>
|
|
|
|
<p> </p>
|
|
|
|
<p><meta charset="UTF-8" />注意:本题与主站 814 题相同:<a href="https://leetcode-cn.com/problems/binary-tree-pruning/">https://leetcode-cn.com/problems/binary-tree-pruning/</a></p>
|