# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + active=""
# 说明：
#     给定二叉树根结点 root ，此外树的每个结点的值要么是 0，要么是 1。
#     返回移除了所有不包含 1 的子树的原二叉树。
#     ( 节点 X 的子树为 X 本身，以及所有 X 的后代。)    

# + active=""
# 示例1:
# 输入: [1,null,0,0,1]
# 输出: [1,null,0,null,1]
#  
# 解释: 
#     只有红色节点满足条件“所有不包含 1 的子树”。
#     右图为返回的答案。
# -

# <img src='https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/06/1028_2.png' width=500>

# + active=""
# 示例2:
#     输入: [1,0,1,0,0,0,1]
#     输出: [1,null,1,null,1]
# -

# <img src='https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/06/1028_1.png' width=550>

# + active=""
# 示例3:
#     输入: [1,1,0,1,1,0,1,0]
#     输出: [1,1,0,1,1,null,1]
# -

# <img src='https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/05/1028.png'>

# + active=""
# 说明:
#     1、给定的二叉树最多有 100 个节点。
#     2、每个节点的值只会为 0 或 1 。
# -

class Solution:
    def pruneTree(self, root: TreeNode) -> TreeNode:
        def help(node):
            if node is None:
                return None
            node.left = help(node.left)
            node.right = help(node.right)
            if node.left is None and node.right is None and node.val == 0:
                return None
            return node
        
        return help(root)

StarcoderdataJupyterScriptsDedupFiltered