0% found this document useful (0 votes)
14 views2 pages

BstII Logic

The document outlines an algorithm for generating all unique binary search trees (BSTs) for a given range of numbers using a recursive function. It describes the base case, the process of iterating over possible roots, combining subtrees, and returning the result. An example walkthrough with n=3 illustrates how the function constructs trees step-by-step.

Uploaded by

abhireddie65
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

BstII Logic

The document outlines an algorithm for generating all unique binary search trees (BSTs) for a given range of numbers using a recursive function. It describes the base case, the process of iterating over possible roots, combining subtrees, and returning the result. An example walkthrough with n=3 illustrates how the function constructs trees step-by-step.

Uploaded by

abhireddie65
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Algorithm: Generate Unique BSTs-II

1. Define Recursive Function:


○ Name: generateTrees(start, end)
○ Input:
■ start: The smallest number in the current range.
■ end: The largest number in the current range.
○ Output:
■ A list of all unique BSTs that can be formed using numbers in the
range [start, end].
2. Base Case:
○ If start > end, return [None]:
■ This represents an empty subtree.
3. Iterate Over Possible Roots:
○ For each number i in the range [start, end]:
■ Treat i as the root of the current tree.
■ Recursively generate:
■ All left subtrees using numbers in the range [start, i-1].
■ All right subtrees using numbers in the range [i+1, end].
4. Combine Subtrees:
○ For each combination of left and right subtrees:
■ Create a new tree with i as the root.
■ Attach one left subtree to the root’s left child.
■ Attach one right subtree to the root’s right child.
○ Add the new tree to the result list.
5. Return the Result:
○ After iterating through all possible roots, return the list of unique trees.
6. Main Function:
○ Call generateTrees(1, n) to generate all unique BSTs for numbers 1 to
n.

Example Walkthrough: n = 3

1. Initial Call: generateTrees(1, 3)


○ Roots: {1, 2, 3}.
○ Recursively construct left and right subtrees for each root.
2. Root = 1:
○ Left subtree: generateTrees(1, 0) → [None].
○ Right subtree: generateTrees(2, 3) → Trees with roots {2, 3}.
3. Root = 2:
○ Left subtree: generateTrees(1, 1) → Tree with root 1.
○ Right subtree: generateTrees(3, 3) → Tree with root 3.
4. Root = 3:
○ Left subtree: generateTrees(1, 2) → Trees with roots {1, 2}.
○ Right subtree: generateTrees(4, 3) → [None].
5. Combine:
○ For each root, combine the generated left and right subtrees to form unique
BSTs.

Key Insights:

1. Recursive Construction:
○ Dividing numbers into left and right subtrees ensures no duplicates.
2. Base Case Handles Empty Subtrees:
○ Returning [None] for invalid ranges allows trees with missing children.

You might also like