Skip to content

Latest commit

 

History

History
52 lines (37 loc) · 1.33 KB

_1325. Delete Leaves With a Given Value.md

File metadata and controls

52 lines (37 loc) · 1.33 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : August 22, 2024

Last updated : August 22, 2024


Related Topics : Tree, Depth-First Search, Binary Tree

Acceptance Rate : 77.35 %


Solutions

Python

# 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: Optional[TreeNode], target: int) -> Optional[TreeNode]:
        def dfs(curr: Optional[TreeNode]) -> Optional[TreeNode] :
            if not curr :
                return None

            if curr.left :
                curr.left = dfs(curr.left)
            if curr.right :
                curr.right = dfs(curr.right)

            if not curr.left and not curr.right and curr.val == target :
                return None

            return curr

        return dfs(root)