Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions TreeFromPreorderInorder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def buildTree(self, preorder, inorder):
"""
:type preorder: List[int]
:type inorder: List[int]
:rtype: Optional[TreeNode]
"""
if not preorder or not inorder:
return None
"""
rootVal = preorder[0]
rootIdx = -1
for i in range(len(inorder)):
if inorder[i] == rootVal:
rootIdx = i
break

root = TreeNode(rootVal)
leftPre = preorder[1:rootIdx+1]
rightPre = preorder[rootIdx+1:]
leftIno = inorder[0:rootIdx]
rightIno = inorder[rootIdx+1:]

root.left = self.buildTree(leftPre, leftIno)
root.right = self.buildTree(rightPre, rightIno)

return root
"""

self.idxMap = dict()
for i in range(len(inorder)):
self.idxMap[inorder[i]] = i

self.treeRootIdx = 0

def helper(preorder, start, end):
if start > end:
#Breached. The current Node is NULL.
return None

#First things first, let us get the root node.
rootVal = preorder[self.treeRootIdx]
rootIdx = self.idxMap[rootVal]
self.treeRootIdx += 1
root = TreeNode(rootVal)


#Let us build the left and right child recursively.
#for left child, start remains the same, but end is the rootIdx - 1
root.left = helper(preorder, start, rootIdx - 1)

#for right child, start would be rootIdx+1, end remains the same.
root.right = helper(preorder, rootIdx + 1, end)

return root

return helper(preorder, 0, len(inorder)-1)

#TC: O(n)
#SC: O(1)
63 changes: 63 additions & 0 deletions ValidateBst.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def isValidBST(self, root):
"""
:type root: Optional[TreeNode]
:rtype: bool
"""
if not root:
return True
"""
self.breachFound=True
self.prev = None
def helper(root):
if not root:
return

helper(root.left)
#Check with the previous value. Why previous?
# Inorder traversal of a BST will always give us
# the sorted value.
if (self.prev != None and self.prev.val >= root.val):
#breach
self.breachFound = False
return

#This node looks good. Update prev and explore right subtree.
self.prev = root
helper(root.right)

return
helper(root)
return self.breachFound
"""

"""
TC : O(n) <--- all the nodes are visited once at max.
SC : O(h) <--- height of the tree. Worst case, it will be O(n)
in case of a skewed tree.
"""
def helper(root, minVal, maxVal):
if not root:
return True

if (root.val <= minVal or root.val >= maxVal):
return False
# If we are going left in a BST, all the numbers on the left subtree
# should be lesser that the currentValue. There is no bound of minimum
# as we don;t care about it.
left = helper(root.left, minVal, root.val)
# If we are going right in a BST, all the numbers on the right subtree
# should be greater that the currentValue. There is no bound of maximum
# as we don;t care about it.
right = helper(root.right, root.val, maxVal)

#Return True if no breaches found.
return left and right

return helper(root, float('-inf'), float('inf'))