forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_in_range.py
More file actions
28 lines (22 loc) · 942 Bytes
/
Copy pathprint_in_range.py
File metadata and controls
28 lines (22 loc) · 942 Bytes
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
from typing import Optional
from tree_node import Node
def print_in_range(root: Optional[Node], k1: int, k2: int) -> None:
"""This function prints the nodes in a BST that are in the range k1 to k2 inclusive"""
# If the tree is empty, return
if root is None:
return
# If the root value is in the range, print the root value
if k1 <= root.data <= k2:
print_in_range(root.left, k1, k2)
print(root.data)
print_in_range(root.right, k1, k2)
# If the root value is less than k1, the nodes in the range will be in the right subtree
elif root.data < k1:
print_in_range(
root.right, k1, k2
) # Fixed: original had left, which is incorrect
# If the root value is greater than k2, the nodes in the range will be in the left subtree
else:
print_in_range(
root.left, k1, k2
) # Fixed: original had right, which is incorrect