-
Notifications
You must be signed in to change notification settings - Fork 0
/
1325.py
49 lines (37 loc) · 1.12 KB
/
1325.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'''1325. Delete Leaves With a Given Value
Created on 2024-05-25 17:23:41
2024-05-25 18:59:34
@author: MilkTea_shih
'''
#%% Packages
from typing import Optional
#%% Variable
# 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
#%% Functions
# 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]:
if root is None:
return None
root.left = self.removeLeafNodes(root.left, target)
root.right = self.removeLeafNodes(root.right, target)
# `root.val == target` is the most important condition.
if root.val == target and root.left is None and root.right is None:
return None
return root
#%% Main Function
#%% Main
if __name__ == '__main__':
pass
#%%