Skip to content

Commit fd4b90d

Browse files
committed
Time: 43 ms (75.33%), Space: 17.1 MB (80.39%) - LeetHub
1 parent 353b358 commit fd4b90d

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from typing import Optional
2+
3+
4+
class TreeNode:
5+
def __init__(self, val=0, left=None, right=None):
6+
self.val = val
7+
self.left = left
8+
self.right = right
9+
10+
11+
class Solution:
12+
def removeLeafNodes(self, root: Optional[TreeNode], target: int) -> Optional[TreeNode]:
13+
if not root :
14+
return None
15+
root.left = self.removeLeafNodes(root.left, target)
16+
root.right = self.removeLeafNodes(root.right, target)
17+
if not root.left and not root.right and root.val == target:
18+
return None
19+
return root
20+
21+
22+
root = TreeNode(1)
23+
root.left = TreeNode(2)
24+
root.left.left = TreeNode(2)
25+
root.right = TreeNode(3)
26+
root.right.left = TreeNode(2)
27+
root.right.right = TreeNode(4)
28+
target = 2
29+
print(Solution().removeLeafNodes(root, target))

0 commit comments

Comments
 (0)