## 0x02 解法

`public class TreeNode {    public var val: Int    public var left: TreeNode?    public var right: TreeNode?    public init() { self.val = 0; self.left = nil; self.right = nil; }    public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }    public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {        self.val = val        self.left = left        self.right = right    }}`

`func pruneTree(_ root: TreeNode?) -> TreeNode? {    func trim(_ root: TreeNode?) -> Bool {        guard let root = root else {            return false        }        let left = trim(root.left)        let right = trim(root.right)                // 因为要从叶子节点开始剪，所以使用后序遍历方式        // 左子树中全是 0        if left == false {            root.left = nil        }        // 右子树中全是 0        if right == false {            root.right = nil        }                // 左子树，右子树，节点，整个子树是否全为 0        return left || right || (root.val == 1)    }        // 整棵树都为 0，则返回 nil    let b = trim(root)    return b ? root : nil}`

## 0x03 我的作品

`​App Store​`​ 搜索即可~