美文网首页
814. 二叉树剪枝

814. 二叉树剪枝

作者: 江北_c2b1 | 来源:发表于2018-09-13 15:47 被阅读0次

    采用后序遍历的方式实现剪枝
    class Solution(object):

    def pruneTree(self, root):
        """
        :type root: TreeNode
        :rtype: TreeNode
        """
        if not root: return
        root.left = self.pruneTree(root.left)
        root.right = self.pruneTree(root.right)
        if not root.left and not root.right and root.val == 0:
            return None
        return root·

    相关文章

      网友评论

          本文标题:814. 二叉树剪枝

          本文链接:https://www.haomeiwen.com/subject/ifnwgftx.html