Leetcode 1450 Solution

This article provides solution to leetcode question 1450 (delete-leaves-with-a-given-value)

https://leetcode.com/problems/delete-leaves-with-a-given-value

Solution

# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def removeLeafNodes(self, root: TreeNode, target: int) -> TreeNode: if not root: return None
root.left = self.removeLeafNodes(root.left, target) root.right = self.removeLeafNodes(root.right, target)
if not root.left and not root.right and root.val == target: return None else: return root