# 1008. Construct Binary Search Tree from Preorder Traversal

**<font color=red>难度: Medium</font>**

## 刷题内容

> 原题连接

* https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/

> 内容描述

```
Return the root node of a binary search tree that matches the given preorder traversal.

(Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a value < node.val, and any descendant of node.right has a value > node.val.  Also recall that a preorder traversal displays the value of the node first, then traverses node.left, then traverses node.right.)

 

Example 1:

Input: [8,5,1,7,10,12]
Output: [8,5,10,1,7,null,12]

 

Note: 

1 <= preorder.length <= 100
The values of preorder are distinct.
```

## 解题方案

> 思路 1
******- 时间复杂度: O(N)******- 空间复杂度: O(N)******

sb题没什么好说的

```python
class Solution:
    def bstFromPreorder(self, preorder: List[int]) -> TreeNode:
        if not preorder:
            return None
        root = TreeNode(preorder[0])
        lefts = [i for i in preorder[1:] if i < preorder[0]]
        rights = [i for i in preorder[1:] if i > preorder[0]]
        root.left = self.bstFromPreorder(lefts)
        root.right = self.bstFromPreorder(rights)
        return root
```




























