-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructBinarySearchTreeFromPreorderTraversal.py
More file actions
37 lines (27 loc) · 1.29 KB
/
constructBinarySearchTreeFromPreorderTraversal.py
File metadata and controls
37 lines (27 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# problem: https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/
# Runtime: 36 ms, faster than 81.10% of Python3 online submissions for Construct Binary Search Tree from Preorder Traversal.
# Memory Usage: 14.4 MB, less than 44.72% of Python3 online submissions for Construct Binary Search Tree from Preorder Traversal.
from typing import List
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def traverse(self, parent_value):
if self.index == len(self.preorder):
return None
val = self.preorder[self.index]
self.index += 1
inbounds = self.index < len(self.preorder)
left = self.traverse(
val) if inbounds and self.preorder[self.index] < val else None
inbounds = self.index < len(self.preorder)
right = self.traverse(max(val, parent_value)) if inbounds and (
val < self.preorder[self.index] < parent_value) else None
return TreeNode(val, left, right)
def bstFromPreorder(self, preorder: List[int]) -> Optional[TreeNode]:
self.index = 0
self.preorder = preorder
return self.traverse(float("inf"))