Skip to content

Commit d0fcbc6

Browse files
authored
Create delete-leaves-with-a-given-value.py
1 parent 5a7df90 commit d0fcbc6

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Time: O(n)
2+
# Space: O(h)
3+
4+
# Definition for a binary tree node.
5+
class TreeNode(object):
6+
def __init__(self, x):
7+
self.val = x
8+
self.left = None
9+
self.right = None
10+
11+
12+
class Solution(object):
13+
def removeLeafNodes(self, root, target):
14+
"""
15+
:type root: TreeNode
16+
:type target: int
17+
:rtype: TreeNode
18+
"""
19+
if not root:
20+
return None
21+
root.left = self.removeLeafNodes(root.left, target)
22+
root.right = self.removeLeafNodes(root.right, target)
23+
return None if root.left == root.right and root.val == target else root

0 commit comments

Comments
 (0)