Advertisement

Construct Binary Search Tree from Preorder Traversal - LeetCode 1008 Solution

Construct Binary Search Tree from Preorder Traversal - Complete Solution Guide

Construct Binary Search Tree from Preorder Traversal is LeetCode problem 1008, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an array of integers preorder, which represents the preorder traversal of a BST (i.e., binary search tree ), construct the tree and return its root . It is guaranteed that there is always possible to find a binary search tree with the given requirements for the given test cases. A binary search tree is a binary tree where for every node, any descendant of Node.left has a value strictly less than Node.val , and any descendant of Node.right has a value strictly greater than Node.val . A preo

Detailed Explanation

The problem asks us to construct a Binary Search Tree (BST) given its preorder traversal. A preorder traversal visits the root node first, then the left subtree, and then the right subtree. The input is an array of integers representing the preorder traversal. The output is the root node of the constructed BST. The BST property ensures that for every node, all values in its left subtree are strictly less than the node's value, and all values in its right subtree are strictly greater than the node's value. The problem guarantees that a valid BST can always be constructed from the given preorder array.

Solution Approach

The provided solution uses a recursive approach with a helper function. The helper function takes the preorder array and an upper bound as input. The upper bound represents the maximum value that a node in the current subtree can have. The recursion starts with an upper bound of infinity (or INT_MAX in C). For each recursive call, if the current value in the preorder array is greater than the upper bound, it means that the current value does not belong to the current subtree, and we return null. Otherwise, we create a new node with the current value, increment the index, and recursively build the left and right subtrees. The left subtree's upper bound is the current node's value, and the right subtree's upper bound is the original upper bound.

Step-by-Step Algorithm

  1. Step 1: Initialize a global index variable to 0. This variable tracks the current position in the preorder array.
  2. Step 2: Define a recursive helper function that takes the preorder array and an upper bound as input.
  3. Step 3: Inside the helper function, check if the index has reached the end of the array or if the current value in the array is greater than the upper bound. If either of these conditions is true, return null.
  4. Step 4: Create a new TreeNode with the current value in the preorder array and increment the index.
  5. Step 5: Recursively call the helper function to build the left subtree. The upper bound for the left subtree is the value of the current node.
  6. Step 6: Recursively call the helper function to build the right subtree. The upper bound for the right subtree is the original upper bound.
  7. Step 7: Return the current node.

Key Insights

  • Insight 1: The first element of the preorder array is always the root of the BST.
  • Insight 2: The BST property can be used to determine the boundaries for the left and right subtrees. Specifically, the left subtree will contain values smaller than the root, and the right subtree will contain values larger than the root.
  • Insight 3: Recursion is a natural way to approach this problem. We recursively construct the left and right subtrees.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Stack, Tree, Binary Search Tree, Monotonic Stack, Binary Tree.

Companies

Asked at: Akamai.