idx
to 0, which will track the position in the preorder list.helper(lower, upper)
that will construct the BST:
a. If idx
is equal to the length of the preorder list, return None
as the tree is constructed.
b. Get the current value val
from the preorder list using idx
.
c. If val
is not within the lower
and upper
limits, return None
.
d. Increment idx
to move to the next element in the preorder list.
e. Create a new tree node with the value val
.
f. Recursively call helper(lower, val)
to construct the left subtree.
g. Recursively call helper(val, upper)
to construct the right subtree.
h. Return the constructed tree node.helper(float('-inf'), float('inf'))
to start the recursion with the initial limits and construct the BST.