Skip to content

Commit 10d2fbf

Browse files
authored
Merge pull request neetcode-gh#1463 from sharansalian/python/669
Add 669-Trim-a-Binary-Search-Tree.py
2 parents 9fea5bd + 71a04d7 commit 10d2fbf

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
9+
if not root:
10+
return None
11+
12+
if root.val > high:
13+
return self.trimBST(root.left, low, high)
14+
15+
if root.val < low:
16+
return self.trimBST(root.right, low, high)
17+
18+
else:
19+
root.left = self.trimBST(root.left, low, high)
20+
root.right = self.trimBST(root.right, low, high)
21+
return root

0 commit comments

Comments
 (0)