|
| 1 | +/* |
| 2 | + Explanation: |
| 3 | +
|
| 4 | + Approach 1: |
| 5 | +
|
| 6 | + The ReconstructBst function takes a slice preOrderTraversalValues which represents the pre-order traversal of a binary search tree. |
| 7 | + It reconstructs the BST using a recursive approach. Here's how the algorithm works: |
| 8 | +
|
| 9 | + The base case is defined when the preOrderTraversalValues slice is empty, in which case it returns nil indicating an empty tree. |
| 10 | +
|
| 11 | + The first element in the preOrderTraversalValues slice represents the current node value of the BST. |
| 12 | +
|
| 13 | + The algorithm finds the index (rightSubTreeRootIdx) where the right subtree starts by iterating over the remaining elements in |
| 14 | + the preOrderTraversalValues slice and finding the first value greater than or equal to the current value. |
| 15 | +
|
| 16 | + It recursively calls ReconstructBst on the sub-array representing the left subtree (preOrderTraversalValues[1:rightSubTreeRootIdx]) |
| 17 | + to reconstruct the left subtree. |
| 18 | +
|
| 19 | + It recursively calls ReconstructBst on the sub-array representing the right subtree (preOrderTraversalValues[rightSubTreeRootIdx:]) |
| 20 | + to reconstruct the right subtree. |
| 21 | +
|
| 22 | + Finally, it creates a new BST node with the current value, the reconstructed left subtree, and the reconstructed right subtree, |
| 23 | + and returns the node. |
| 24 | +
|
| 25 | + The algorithm builds the BST in a top-down manner by dividing the pre-order traversal values into left and right subtrees. |
| 26 | + It constructs the left subtree first and then the right subtree. |
| 27 | +
|
| 28 | + The time complexity of the algorithm is O(n^2) in the worst case, where n is the number of nodes in the BST. |
| 29 | +
|
| 30 | +
|
| 31 | + ****************************************************************************************** |
| 32 | +
|
| 33 | + Approach 2: |
| 34 | +
|
| 35 | + The ReconstructBst function takes a slice preOrderTraversalValues which represents the pre-order traversal of a binary search tree. |
| 36 | + It reconstructs the BST using a range-based approach. Here's how the algorithm works: |
| 37 | +
|
| 38 | + The ReconstructBst function initializes a treeInfo struct to keep track of the current root index. |
| 39 | +
|
| 40 | + The ReconstructBst function calls the reconstructBSTFromRange helper function, passing the minimum and maximum integer values |
| 41 | + as the initial range, the pre-order traversal values, and the treeInfo struct. |
| 42 | +
|
| 43 | + The reconstructBSTFromRange function first checks if the current root index has reached the end of the pre-order traversal values. |
| 44 | + If so, it returns nil indicating an empty subtree. |
| 45 | +
|
| 46 | + It retrieves the value of the current root from the pre-order traversal values. |
| 47 | +
|
| 48 | + It checks if the root value is outside the valid range defined by the lower and upper bounds. If so, it returns |
| 49 | +
|
| 50 | + The time complexity of the ReconstructBst function is O(n), where n is the number of nodes in the reconstructed BST. |
| 51 | + This is because the function processes each node exactly once. |
| 52 | +
|
| 53 | + The space complexity of the ReconstructBst function is O(n), where n is the number of nodes in the reconstructed BST. |
| 54 | + This is because the function creates BST nodes and recursively calls itself to construct the left and right subtrees. |
| 55 | + The space complexity is determined by the height of the BST, which can be at most n in the worst case for a skewed BST. |
| 56 | +*/ |
1 | 57 | package main
|
2 | 58 |
|
3 | 59 | import "math"
|
|
0 commit comments