Skip to content

Commit

Permalink
third commit
Browse files Browse the repository at this point in the history
  • Loading branch information
suhassuhas committed May 5, 2022
1 parent 60e640f commit 82ecbf0
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Leetcode/May-Day-01-2022-Backspace-String.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@


class Solution:
# Time - Space = O(n),O(n)
def backspaceCompare(self, s: str, t: str) -> bool:
def build(s):
ans = []
Expand All @@ -37,6 +38,7 @@ def build(s):
return "".join(ans)
return build(s) == build(t)

# Time - Space = O(n),O(1)
def backspaceCompare1(self,s:str,t:str) -> bool:
def getNextChar(string,pointer):
num_of_backspaces = 0
Expand Down
55 changes: 55 additions & 0 deletions Topic-Wise/Binary_Search_Tree/reconstrutBST.py
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)
20 changes: 20 additions & 0 deletions Topic-Wise/Binary_Search_Tree/validateBST.py
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)

0 comments on commit 82ecbf0

Please sign in to comment.