Construct Binary Search Tree from Preorder Traversal
Given an array of distinct integers that is the preorder traversal of a binary search tree, construct the tree and return its root. In a BST, every node in the left subtree is smaller than the node and every node in the right subtree is larger. Preorder visits a node, then its left subtree, then its right subtree.
30:00
Construct Binary Search Tree from Preorder Traversal
Given an array of distinct integers that is the preorder traversal of a binary search tree, construct the tree and return its root. In a BST, every node in the left subtree is smaller than the node and every node in the right subtree is larger. Preorder visits a node, then its left subtree, then its right subtree.
Example 1:
Input: {"preorder":[8,5,1,7,10,12]}
Output: [8,5,10,1,7,null,12]
Constraints:
1≤ preorder.length ≤100
1≤ preorder[i] ≤1000
All the values of preorder are unique.
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"preorder":[8,5,1,7,10,12]}
Read the preorder array left to right with one pointer i. Each call build(bound) may only take values below its bound; the first call has no bound.