Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit10d2fbf

Browse files
authored
Merge pull requestneetcode-gh#1463 from sharansalian/python/669
Add 669-Trim-a-Binary-Search-Tree.py
2 parents9fea5bd +71a04d7 commit10d2fbf

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+
classSolution:
8+
deftrimBST(self,root:Optional[TreeNode],low:int,high:int)->Optional[TreeNode]:
9+
ifnotroot:
10+
returnNone
11+
12+
ifroot.val>high:
13+
returnself.trimBST(root.left,low,high)
14+
15+
ifroot.val<low:
16+
returnself.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+
returnroot

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp