Skip to content

Latest commit

 

History

History
25 lines (23 loc) · 705 Bytes

814.md

File metadata and controls

25 lines (23 loc) · 705 Bytes

814. Binary Tree Pruning

Solution 1 (time O(n), space O(1))

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def pruneTree(self, root):
        """
        :type root: Optional[TreeNode]
        :rtype: Optional[TreeNode]
        """
        if not root:
            return root
        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