Skip to content

Commit 8cea85e

Browse files
committed
add approach 2 with better time complexity
1 parent ae94c40 commit 8cea85e

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

Trees/Binary Search Trees/reconstruct_bst.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package main
22

3+
import "math"
4+
35
// BST represents a binary search tree node.
46
type BST struct {
57
Value int
68
Left *BST
79
Right *BST
810
}
911

12+
// Approach 1: Time complexity O(n^2) Space O(n), where n is length of input array
1013
// ReconstructBst takes a slice of integers representing the pre-order traversal of a BST and returns the reconstructed BST.
1114
func ReconstructBst(preOrderTraversalValues []int) *BST {
1215
// Base case: If the pre-order traversal is empty, return nil indicating an empty tree.
@@ -34,3 +37,48 @@ func ReconstructBst(preOrderTraversalValues []int) *BST {
3437
// Create a new BST node with the current value and the reconstructed left and right subtrees.
3538
return &BST{Value: currentVal, Left: leftSubTree, Right: rightSubTree}
3639
}
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

Comments
 (0)