From ea9341d10521ea22b3a675165b9f2465df9b4800 Mon Sep 17 00:00:00 2001 From: Pratul Trivedi Date: Thu, 26 Feb 2026 18:52:28 -0800 Subject: [PATCH] Solved Trees-1 --- TreeFromPreorderInorder.py | 66 ++++++++++++++++++++++++++++++++++++++ ValidateBst.py | 63 ++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 TreeFromPreorderInorder.py create mode 100644 ValidateBst.py diff --git a/TreeFromPreorderInorder.py b/TreeFromPreorderInorder.py new file mode 100644 index 00000000..7eee09ca --- /dev/null +++ b/TreeFromPreorderInorder.py @@ -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) \ No newline at end of file diff --git a/ValidateBst.py b/ValidateBst.py new file mode 100644 index 00000000..2c2191a8 --- /dev/null +++ b/ValidateBst.py @@ -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')) \ No newline at end of file