-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtrim.py
More file actions
38 lines (31 loc) · 1.1 KB
/
trim.py
File metadata and controls
38 lines (31 loc) · 1.1 KB
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
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def trimBST(self, root, L, R):
"""
:type root: TreeNode
:type L: int
:type R: int
:rtype: TreeNode
"""
# Each subtree visited we move through, each subtree's root will be node
def trim(node):
# Reaching the depth of a BST
if not node:
return None
# Range should be within L - R, hence if node is less than L, trim the right
elif node.val < L:
return trim(node.right)
# Range should be within L - R, hence if node is greater than R, trim the left
elif node.val > R:
return trim(node.left)
# Else, trim both the sides
else:
node.left = trim(node.left)
node.right = trim(node.right)
return node
return trim(root)