All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : August 22, 2024
Last updated : August 22, 2024
Related Topics : Tree, Depth-First Search, Binary Tree
Acceptance Rate : 77.35 %
# 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)