|
1 | 1 | package main
|
2 | 2 |
|
| 3 | +import "math" |
| 4 | + |
3 | 5 | // BST represents a binary search tree node.
|
4 | 6 | type BST struct {
|
5 | 7 | Value int
|
6 | 8 | Left *BST
|
7 | 9 | Right *BST
|
8 | 10 | }
|
9 | 11 |
|
| 12 | +// Approach 1: Time complexity O(n^2) Space O(n), where n is length of input array |
10 | 13 | // ReconstructBst takes a slice of integers representing the pre-order traversal of a BST and returns the reconstructed BST.
|
11 | 14 | func ReconstructBst(preOrderTraversalValues []int) *BST {
|
12 | 15 | // Base case: If the pre-order traversal is empty, return nil indicating an empty tree.
|
@@ -34,3 +37,48 @@ func ReconstructBst(preOrderTraversalValues []int) *BST {
|
34 | 37 | // Create a new BST node with the current value and the reconstructed left and right subtrees.
|
35 | 38 | return &BST{Value: currentVal, Left: leftSubTree, Right: rightSubTree}
|
36 | 39 | }
|
| 40 | + |
| 41 | + |
| 42 | +// Approach 2: Time complexity O(n) Space O(n), where n is length of input array |
| 43 | + |
| 44 | +// treeInfo is a helper struct to keep track of the current root index during the reconstruction process. |
| 45 | +type treeInfo struct { |
| 46 | + rootIdx int |
| 47 | +} |
| 48 | + |
| 49 | +// ReconstructBst takes a slice of integers representing the pre-order traversal of a BST and returns the reconstructed BST. |
| 50 | +func ReconstructBst2(preOrderTraversalValues []int) *BST { |
| 51 | + // Create a treeInfo struct to keep track of the current root index. |
| 52 | + treeInfo := &treeInfo{rootIdx: 0} |
| 53 | + |
| 54 | + // Call the helper function to reconstruct the BST from the given range and return the result. |
| 55 | + return reconstructBSTFromRange(math.MinInt32, math.MaxInt32, preOrderTraversalValues, treeInfo) |
| 56 | +} |
| 57 | + |
| 58 | +// reconstructBSTFromRange reconstructs the BST recursively within the given range using the pre-order traversal values. |
| 59 | +func reconstructBSTFromRange(lowerBound, upperBound int, preOrderTraversalValues []int, currentSubtreeInfo *treeInfo) *BST { |
| 60 | + // Check if the root index has reached the end of the pre-order traversal values. If so, return nil indicating an empty subtree. |
| 61 | + if currentSubtreeInfo.rootIdx == len(preOrderTraversalValues) { |
| 62 | + return nil |
| 63 | + } |
| 64 | + |
| 65 | + // Get the value of the current root from the pre-order traversal values. |
| 66 | + rootValue := preOrderTraversalValues[currentSubtreeInfo.rootIdx] |
| 67 | + |
| 68 | + // Check if the root value is out of the valid range defined by the lower and upper bounds. If so, return nil indicating an invalid subtree. |
| 69 | + if rootValue < lowerBound || rootValue >= upperBound { |
| 70 | + return nil |
| 71 | + } |
| 72 | + |
| 73 | + // Increment the root index to move to the next element in the pre-order traversal values. |
| 74 | + currentSubtreeInfo.rootIdx++ |
| 75 | + |
| 76 | + // Recursively reconstruct the left subtree within the range (lowerBound, rootValue) using the updated root index. |
| 77 | + leftSubtree := reconstructBSTFromRange(lowerBound, rootValue, preOrderTraversalValues, currentSubtreeInfo) |
| 78 | + |
| 79 | + // Recursively reconstruct the right subtree within the range (rootValue, upperBound) using the updated root index. |
| 80 | + rightSubtree := reconstructBSTFromRange(rootValue, upperBound, preOrderTraversalValues, currentSubtreeInfo) |
| 81 | + |
| 82 | + // Create a new BST node with the current root value and the reconstructed left and right subtrees. |
| 83 | + return &BST{Value: rootValue, Left: leftSubtree, Right: rightSubtree} |
| 84 | +} |
0 commit comments