-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtrim-a-binary-search-tree.py
90 lines (70 loc) · 2.43 KB
/
trim-a-binary-search-tree.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from typing import Optional, List, Tuple
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def trimBST(
self, root: Optional[TreeNode], low: int, high: int
) -> Optional[TreeNode]:
if not root:
return None
new_root = TreeNode(root.val)
new_root.right = root
stack: List[Tuple[Optional[TreeNode], Optional[TreeNode], TreeNode]] = [
(new_root, new_root, root)
]
while stack:
parent_left, parent_right, node = stack.pop()
if low <= node.val <= high:
if parent_left:
parent_left.left = node
if parent_right:
parent_right.right = node
if node.left:
if low <= node.val <= high:
stack.append((node, None, node.left))
node.left = None
else:
stack.append((parent_left, parent_right, node.left))
if node.right:
if low <= node.val <= high:
stack.append((None, node, node.right))
node.right = None
else:
stack.append((parent_left, parent_right, node.right))
return new_root.left or new_root.right
def trimBSTRecursive(
self, root: Optional[TreeNode], low: int, high: int
) -> Optional[TreeNode]:
def dfs(node: TreeNode) -> TreeNode:
if node.left:
node.left = dfs(node.left)
if node.right:
node.right = dfs(node.right)
if low <= node.val <= high:
return node
elif node.val < low:
return node.right
else:
return node.left
if not root:
return None
return dfs(root)
def trimBSTFirst(self, root: TreeNode, L: int, R: int) -> TreeNode:
if not root:
return None
def dfs(node):
if node.left:
node.left = dfs(node.left)
if node.right:
node.right = dfs(node.right)
if node.val < L:
return node.right
elif node.val > R:
return node.left
else:
return node
return dfs(root)