-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
60e640f
commit 82ecbf0
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Given preorder traversal of a binary search tree, construct the BST. | ||
|
||
# For example, if the given traversal is {10, 5, 1, 7, 40, 50}, then the output should be the root of the following tree. | ||
|
||
# 10 | ||
# / \ | ||
# 5 40 | ||
# / \ \ | ||
# 1 7 50 | ||
|
||
# This is an input class. Do not edit. | ||
class BST: | ||
def __init__(self, value, left=None, right=None): | ||
self.value = value | ||
self.left = left | ||
self.right = right | ||
|
||
|
||
def reconstructBst(preOrderTraversalValues): | ||
# Write your code here. | ||
n = len(preOrderTraversalValues) | ||
if n == 0: | ||
return None | ||
rightIdx = n | ||
for i in range(1,n): | ||
if preOrderTraversalValues[0] <= preOrderTraversalValues[i]: | ||
rightIdx = i | ||
break | ||
leftpotv = reconstructBst(preOrderTraversalValues[1:rightIdx]) | ||
rightpotv = reconstructBst(preOrderTraversalValues[rightIdx:]) | ||
return BST(preOrderTraversalValues[0],leftpotv,rightpotv) | ||
|
||
|
||
|
||
|
||
class TreeInfo: | ||
def __init__(self,rootIdx): | ||
self.rootIdx = rootIdx | ||
|
||
def reconstructBst1(preOrderTraversalValues): | ||
# Write your code here. | ||
treeInfo = TreeInfo(0) | ||
return reconstructBstRange(float("-inf"),float("inf"),preOrderTraversalValues,treeInfo) | ||
|
||
def reconstructBstRange(lowerBound,upperBound,preOrderTraversalValues,currentSubtreeInfo): | ||
if currentSubtreeInfo.rootIdx == len(preOrderTraversalValues): | ||
return None | ||
|
||
rootValue = preOrderTraversalValues[currentSubtreeInfo.rootIdx] | ||
if rootValue < lowerBound or rootValue >= upperBound: | ||
return None | ||
currentSubtreeInfo.rootIdx += 1 | ||
leftSubtree = reconstructBstRange(lowerBound,rootValue,preOrderTraversalValues,currentSubtreeInfo) | ||
rightSubtree = reconstructBstRange(rootValue,upperBound,preOrderTraversalValues,currentSubtreeInfo) | ||
return BST(rootValue,leftSubtree,rightSubtree) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# 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 isValidBST(self, root: Optional[TreeNode]) -> bool: | ||
return self.isValidBSTHelper(root,float("-inf"),float("inf")) | ||
|
||
def isValidBSTHelper(self,root,minValue,maxValue): | ||
if root == None: | ||
return True | ||
if root.left != None and root.val <= root.left.val: | ||
return False | ||
if root.right != None and root.val >= root.right.val: | ||
return False | ||
if root.val <= minValue or root.val >= maxValue: | ||
return False | ||
return self.isValidBSTHelper(root.left,minValue,root.val) and self.isValidBSTHelper(root.right,root.val,maxValue) |