LeetCode 1008 Construct Binary Search Tree from Preorder Traversal (Python)

Posted by 小明MaxMing on April 26, 2020

题目

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.)

解题思路

代码

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def bstFromPreorder(self, preorder: List[int]) -> TreeNode:
        def dfs(l, r):
            if l == r:
                return TreeNode(preorder[l])
            if l > r:
                return None
            val = preorder[l]
            node = TreeNode(val)
            pos = l + 1
            while pos < self.n and preorder[pos] < val:
                pos += 1
            node.left = dfs(l + 1, pos - 1)
            node.right = dfs(pos, r)
            return node
        
        self.n = len(preorder)
        return dfs(0, self.n - 1)

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅